使用ArrayAdapter for ListView的OnItemClickListener

Ahm*_*far 21 android listview onclick android-arrayadapter onitemclicklistener

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

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

List<Comment> values = datasource.some_search("Wednesday","11");
        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

我如何实现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<Comment> values = datasource.search("Wednesday","11");


        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

    }
Run Code Online (Sandbox Code Playgroud)

编辑3:XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add New" 
            android:onClick="onClick"/>

        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete First" 
            android:onClick="onClick"/>

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout> 
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 63

使用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
      }
   });
Run Code Online (Sandbox Code Playgroud)

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

你也覆盖onItemClick.第一个参数是适配器.第二个参数就是观点.第三个参数是位置(listview项的索引).

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

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

假设您的活动延伸 ListActivtiy

     setListAdapter(adapter); 
Run Code Online (Sandbox Code Playgroud)

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

     ListView lv = (ListView) findViewById(R.id.listview1);
     //initialize adapter 
     lv.setAdapter(adapter); 
Run Code Online (Sandbox Code Playgroud)


Piy*_*ush 6

你可以用这种方式......

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                final int position, long id) {

          String main = listView.getSelectedItem().toString();
        }
    });
Run Code Online (Sandbox Code Playgroud)