OnItemClickListener using ArrayAdapter for ListView

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

This is the code I use to create it:

  • Given a latitude and longitude, Get the location name
  • How to add a bottom menu to Android activity
  • How to disable SSLv3 in android for HttpsUrlConnection?
  • Gradle build failing after update to 3.0
  • how to find the distance between two geopoints?
  • rewriting Java code to JS - creating an audio from bytes?
  • 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);
    

    How do I implement onItemClickListener?

    Thanks!

    EDIT: I am using in my ArrayAdapter and ListView a string of objects.

    EDIT 2: More code:

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

    EDIT 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> 
    

    Related posts:

    Event for Handling the Focus of the EditText
    Making the vertical scrollbar visible
    how to install android studio full offline
    SoftKeyboard hiding EditText
    Android NDK - OpenGL ES Tutorial/Libraries
    Ascertain Google Streetview function existence
  • Android Fragment lifecycle over orientation changes
  • How to re-enable a disabled Android Lint warning on file level
  • java.io.filenotfoundexception open failed eacces (permission denied) on device
  • Android Geocoder quota limits
  • Extending GridLayout
  • What are side effects of disabling LINT inspection?
  • 4 Solutions collect form web for “OnItemClickListener using ArrayAdapter for ListView”

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

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

    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
    
        }
    
    }
    

    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);
    
            }
        });
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.