使用ArrayAdapter for ListView的OnItemClickListener

我想为我使用ArrayAdapter创建的ListView创建一个OnItemClickListener

这是我用来创建它的代码:

List values = datasource.some_search("Wednesday","11"); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); 

我如何实现onItemClickListener?

谢谢!

编辑:我在我的ArrayAdapter和ListView中使用一串对象。

编辑2:更多代码:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datasource = new CommentsDataSource(this); datasource.open(); //check if database is populated if NOT, populate with txtToDb(); if (!datasource.isPopulated()) { // Database is not populated so copy it from assets here try { txtToDb(); Log.i("Database", "Was not Populated"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("Database", "Was not populated: txtToDb(); failed"); } } else { Log.i("Database", "Populated"); } //wat to show on screen: List values = datasource.search("Wednesday","11"); // Use the SimpleCursorAdapter to show the // elements in a ListView ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } 

编辑3:XML:

    

Solutions Collecting From Web of "使用ArrayAdapter for ListView的OnItemClickListener"

使用OnItemClickListener

  ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> adapter, View v, int position, long arg3) { String value = (String)adapter.getItemAtPosition(position); // assuming string and if you want to get the value on click of list item // do what you intend to do on click of listview row } }); 

当您单击某一行时,将触发一个侦听器。 所以你在listview上setOnClickListener并使用匿名内部类OnItemClickListener

您还会覆盖onItemClick 。 第一个参数是适配器。 第二个参数就是观点。 第三个参数是位置(listview项的索引)。

使用该位置即可获得该项目。

编辑:从您的评论我假设您需要设置适配器列表视图

因此,假设您的活动扩展了ListActivtiy

  setListAdapter(adapter); 

或者,如果您的活动类扩展了Activity

  ListView lv = (ListView) findViewById(R.id.listview1); //initialize adapter lv.setAdapter(adapter); 

你可以用这种方式……

 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> parent, View view, final int position, long id) { String main = listView.getSelectedItem().toString(); } }); 

好的,在您的Activity扩展ListActivity的信息之后,这是一种实现OnItemClickListener的方法:

 public class newListView extends ListView { public newListView(Context context) { super(context); } @Override public void setOnItemClickListener( android.widget.AdapterView.OnItemClickListener listener) { super.setOnItemClickListener(listener); //do something when item is clicked } } 

我正在使用arrayadpter,使用这个以后的代码,我能够得到项目

String value =(String)adapter.getItemAtPosition(position);

 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { String string=adapter.getItem(position); Log.d("**********", string); } });