Search code examples
androidlistviewchildviews

Getting listView child on itemClicked


listView.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
    {   
        //Get childViews here and set their click listener from here not from adapter 
        // something like view.forward_icon.setOnClick(...)
}});

I simply like to get the ListView item childs here and set their text or put an event from here not from adapter. can someone guide me how to do this. I have done it before but forget to use the technique. here is my item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <RelativeLayout
        android:id="@+id/items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/listview_bg"
        android:padding="5dp" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Mote"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:padding="5dp"
            android:text="20 March 2015     17:00 to 21:00"
            android:textColor="@android:color/white" />

         <ImageView
            android:id="@+id/forward_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="3dp"
            android:src="@drawable/forward_icon" />
    </RelativeLayout>

</RelativeLayout>

I want to get the forward_icon here and set an onclickListener from here. I can do it from adapter but due to some accessibility issues . I want to do it like this.


Solution

  • I have remember the solution by seeing the answer of @zozelfelfo and the other answer too.

    The correct way of achieving the functionality is

    listView.setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id)
                {           
                    TextView tv = (TextView)view.findViewById(R.id.item_title);
                    // you can also get other listView item childs like this...
                    tv.setOnClickListener(new OnClickListener()
                    {
    
                        @Override
                        public void onClick(View v)
                        {
                            Toast.makeText(context, "Title is Clicked", Toast.LENGTH_LONG).show();
    
                        }
                    });
    
                }});