在点击列表视图项上启动新活动

我需要一些帮助才能在listview项中进行简单的单击以打开一个新的Activity。 我在这里看到了很多这样的问题,但没有人帮助过我。

public class CustomListView extends ListActivity { private EfficientAdapter adap; ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); adap = new EfficientAdapter(this); setListAdapter(adap); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); startActivity(new Intent(CustomListView.this, next.class)); } public static class EfficientAdapter extends BaseAdapter implements Filterable { private LayoutInflater mInflater; private Bitmap mIcon1; private Context context; public EfficientAdapter(Context context) { // Cache the LayoutInflate to avoid asking for a new one each time. mInflater = LayoutInflater.from(context); this.context = context; } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.adaptor_content, null); convertView.setOnClickListener(new OnClickListener() { private int pos = position; @Override public void onClick(View v) { } }); convertView.setTag(holder); }else{ // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } return convertView; } ... } } 

我还尝试从CustomListView类中添加onCreate方法中的下一个代码,但它也不起作用

 ListView lv = getListView(); // listening to single list item on click lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { startActivity(new Intent(CustomListView.this, next.class)); } }); 

CustomListView类中删除onListItemClick() ,并将startActivity()方法放在convertView.setOnClickListener()

 convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getApplicationContext(), two.class)); } }); 

试试这个代码。 我相信它会帮助你,不要将类设置为像这样的xml文件;

 setContentView(R.layout.second); 

确保你确实已经将上面的代码放在了你的课堂上,否则你在打开新活动时会遇到错误。

 package com.exampled.list; import android.os.Bundle; import android.app.ListActivity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { String [] names ={ "Iphone", "Samsung", "Nokia", "Ericsson", "BlackBerry", "Benq" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); setListAdapter(new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,names)); } public void onListItemClick(ListView Parent, View v, int position,long id){ startActivity(new Intent(MainActivity.this, Second.class)); //Toast.makeText(this, "Clicked on : " + names[position], Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

在你的代码中。 one.this是指Context

由于您的Activity的名称是CustomListView ,您应该编写CustomListView.this而不是one.this

您也可以使用getApplicationContext()

尝试使用startActivity(new Intent(one.this, two.class)); – 编码到

 convertView.setOnClickListener(new OnClickListener() { private int pos = position; @Override public void onClick(View v) {...} }); 

这是什么意思? 它应该是一个Context