自定义列表视图中的setOnItemClickListener

时间:2012-09-04 06:42:11

标签: android-listview

以下是我正在设置自定义列表视图的活动ShowData.java。现在我需要单击该列表视图上的视图并需要显示另一个活动但是会强制关闭..它会在{{1}上抛出错误}

listview.setAdapter(adapter);

以下是public class ShowData extends Activity { ListView listview; Intent intent; ArrayList<BeanClass> datalist; CustomAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listview = (ListView) findViewById(R.id.listView1); Intent intent = getIntent(); datalist = (ArrayList<BeanClass>) intent.getSerializableExtra("list"); Log.i("Size of ArrayList", "is " +datalist.size()); adapter = new CustomAdapter(ShowData.this, R.layout.customrow , datalist); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { Intent i = new Intent(ShowData.this,DetailActivity.class); i.putExtra("datalist", datalist); i.putExtra("position", arg2); startActivity(i); } } }

CustomAdapter.java

我应该如何使用setOnItemClickListener ... ???感谢...

1 个答案:

答案 0 :(得分:4)

在你的getView方法中试试:

row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(context,DetailActivity.class);
                i.putExtra("datalist", vbc);
                i.putExtra("position", position);
                startActivity(i);
            }
        });