我怎样才能在ArrayAdapter上设置onClickListener?

我正在像下面这样上课

// All necessary imports are here public class More extends Activity { String[] MoreItems = { "Transfers", "Budgets", "Branches", "Tools", "News", "Customer Service", "Settings", "Help", "About" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.more_view); ListView moreListView = (ListView) findViewById(R.id.moreListView); MoreListAdapter listAdapter = new MoreListAdapter(); moreListView.setAdapter(listAdapter); // accountsTypeListView.setOnItemClickListener(listClickListner); } class MoreListAdapter extends ArrayAdapter<String> { MoreListAdapter() { super(More.this, R.layout.list_item, MoreItems); } public View getView(int position, View convertView, ViewGroup parent) { View row; if (convertView == null) { LayoutInflater inflater = getLayoutInflater(); row = inflater.inflate(R.layout.list_item, parent, false); } else { row = convertView; } TextView tv = (TextView) row.findViewById(R.id.textItem); tv.setText(getItem(position)); return row; } } } 

它会生成列表,我想调用各自的点击各自的活动,如果用户点击转移,然后将显示转移活动,我怎样才能在这个列表上打电话onClickListener,我怎样才能开始点击活动。

Solutions Collecting From Web of "我怎样才能在ArrayAdapter上设置onClickListener?"

你也可以这样做

 moreListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Log.d("############","Items " + MoreItems[arg2] ); } }); 

有两个选项可以处理每行的单击事件。

1)如果你的类扩展了ListActivity ,你可以重写下面的方法。

 @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //do something here using the position in the array } 

2)在getView()方法中处理行的单击事件

 row.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); 

例如:

  ListView lv = getListView(); lv.setAdapter(listAdapter); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view,int position, long id) { Intent i = new Intent(More.this, NextActvity.class); //If you wanna send any data to nextActicity.class you can use i.putExtra(String key, value.get(position)); startActivity(i); } }); 

你的“更多”类必须扩展ListActivity,而不是活动,那么你可以重写onListItemClick

 @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //do something here using the position in the arrya } 

编辑:忘了说,在你的布局你的ListView必须被称为:android:id =“@ android:id / list”