setOnItemClickListener on custom ListView

Related searches

I have a custom ListView. This ListView contains 1 Image and 6 TextViews. To retrieve the value I have created a setOnItemClickListener(...). Whenever I click on the ListView how could I actually retrieve all the data from the 6 TextViews?

Sample Code:

ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      Object listItem = list.getItemAtPosition(position);
   } 
});

In the sample code above, the listItem should contain the selected data for the textView.

Android - List view - setOnItemClickListener, Below is an example of how to use this technique in List Items. To show that this works, I have created a simple ListView which have a text view. I have Implemented a custom ListView by extending LinearLayout for every row. Every row has a small thumbnail, a text and a check box.The list view is deployed properly and I can scroll and fling through it without any problems.

I too had that same problem.. If we think logically little bit we can get the answer.. It worked for me very well.. I hope u will get it..

  1. listviewdemo.xml

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="30dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />
    

  2. listviewcontent.xml - note that TextView - android:id="@+id/txtLstItem"

    <LinearLayout
        android:id="@+id/listviewcontentlayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dp" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/txtLstItem"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:shadowColor="@android:color/black"
                android:shadowRadius="5"
                android:textColor="@android:color/white" />
    
        </LinearLayout>
    
        <ImageView
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dp" />
    </LinearLayout>
    

  3. ListViewActivity.java - Note that view.findViewById(R.id.txtLstItem) - as we setting the value to TextView by setText() method we getting text from TextView by View object returned by onItemClick method. OnItemClick() returns the current view.

    TextView v=(TextView) view.findViewById(R.id.txtLstItem);
    Toast.makeText(getApplicationContext(), "selected Item Name is "+v.getText(), Toast.LENGTH_LONG).show();**
    

    Using this simple logic we can get other values like CheckBox, RadioButton, ImageView etc.

    ListView List = (ListView) findViewById(R.id.listview);
    cursor = cr.query(CONTENT_URI,projection,null,null,null);
    adapter = new ListViewCursorAdapter(ListViewActivity.this, R.layout.listviewcontent, cursor, from, to);
    
    cursor.moveToFirst();
    
    // Let activity manage the cursor
    startManagingCursor(cursor);
    
    List.setAdapter(adapter);
    List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick (AdapterView < ? > adapter, View view,int position, long arg){
                // TODO Auto-generated method stub
                TextView v = (TextView) view.findViewById(R.id.txtLstItem);
                Toast.makeText(getApplicationContext(), "selected Item Name is " + v.getText(), Toast.LENGTH_LONG).show();
            }
        }
    );
    

android.widget.ListView.setOnItemClickListener java code examples , listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener()); Android - Executing a custom listview in a custom dialog properly. On android applications listview supports both type of click functions onclicklistener () and onItemClickListener () but a major difference between both of them is onclicklistener () function set click event on whole list view user can press on any element but only single task it will do but after applying onItemClickListener () on list view it will set custom list array element item click events so developer can perform multiple tasks on every item click options.

If in the listener you get the root layout of the item (say itemLayout), and you gave some id's to the textviews, you can then get them with something like itemLayout.findViewById(R.id.textView1).

AdapterView, list); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @ Override public void onItemClick(AdapterView<?> parent, View´┐Ż I have a list view with two text view and one edit text in each row , list view setOnItemClickListener() is not working. Here My Java Code. public class CreateChallan extends Activity { ListView

If it helps anyone, I found that the problem was I already had an android:onClick event in my layout file (which I inflated for the ListView rows). This was superseding the onItemClick event.

setOnItemClickListener within Custom Adapter class?, Android - List view - setOnItemClickListener - In this series, we are going to learn about the click Listeners in android and how to use them? Submitted by Manu Jemini, on January 26, 2018 Below is an example of how to use this technique in List Items. To show that this works, I have created a simple ListView which have a text view.

If above answers don't work maybe you didn't add return value into getItem method in the custom adapter see this question and check out first answer.

setOnItemClickListener within Custom Adapter class? I've tried setting an OnItemClickListener within the View method of my custom Adapter class. Then I moved it into my Activity as per somebody's suggestion: ListView listView = (ListView)findViewById (android.R.id.list); listView.setOnItemClickListener (new AdapterView.OnItemClickListener () { @Override public void onItemClick (AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent (NotificationsActivity.this,

We are creating a custom ListView of by subclassing ArrayAdapter with the DataModel as the object. getView() is the method that returns the actual view used as a row within the ListView at a particular position. The content_main.xml contains the ListView as shown below. content_main.xml

A ListView control can use a custom view if you set the View property to the resource key. The following example shows how to specify PlainView as the view mode for a ListView . //Set the ListView View property to the tileView custom view lv.View = lv.FindResource("tileView") as ViewBase;

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { click(); } }); the event is never fired. I tried to put as many items in my row not clickable or focusable.

Comments
  • guys i am not able to sense the touch even if i write a toast inside onItemClick it is not showing
  • @abhishek , same issue with me also
  • Thank You very much bigstones.
  • @kangalert you're welcome. remember to mark the accepted answers (click on the checkmark next to the answer), to let people know that's a working solution. it also raises yours and answerer's reputation :)