OnItemClickListener using ArrayAdapter for ListView

后端 未结 4 1565
Happy的楠姐
Happy的楠姐 2020-11-29 05:18

I want to have an OnItemClickListener for a ListView I create using an ArrayAdapter

This is the code I use to create it:

List values =         


        
相关标签:
4条回答
  • 2020-11-29 05:38

    Ok, after the information that your Activity extends ListActivity here's a way to implement 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
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:49

    Use 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
          }
       });
    

    When you click on a row a listener is fired. So you setOnClickListener on the listview and use the annonymous inner class OnItemClickListener.

    You also override onItemClick. The first param is a adapter. Second param is the view. third param is the position ( index of listview items).

    Using the position you get the item .

    Edit : From your comments i assume you need to set the adapter o listview

    So assuming your activity extends ListActivtiy

         setListAdapter(adapter); 
    

    Or if your activity class extends Activity

         ListView lv = (ListView) findViewById(R.id.listview1);
         //initialize adapter 
         lv.setAdapter(adapter); 
    
    0 讨论(0)
  • 2020-11-29 05:55

    you can use this way...

    listView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {
    
              String main = listView.getSelectedItem().toString();
            }
        });
    
    0 讨论(0)
  • 2020-11-29 06:00

    i'm using arrayadpter ,using this follwed code i'm able to get items

    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);
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题