0

I have a file XML with the name list_item.xml that contains the following code:

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="20dp"
      android:layout_weight="2"
      android:orientation="horizontal">


      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:id="@+id/telephone"
            android:src="@drawable/telephone"/>

      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:id="@+id/favorie"
            android:src="@drawable/heart"/>

      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/consulter"
            android:src="@drawable/consulter"/>

</LinearLayout>   

In java code I have the activity :

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultat_recherche);

The code of xml file(resultat_recherche.xml ):

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

I have filled this list with an Adapter :

ListAdapter adapter = new SimpleAdapter(
            Resultat_recherche.this, contactList,
     R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});

setListAdapter(adapter);

Now I want to recover the éléments from list_item (ImageView) to manage the click and start intent.

i want for exemple when i click in imageview(telephone) i will do something

how I can do that in this activity ? Thank you

Luka Jacobowitz
  • 22,795
  • 5
  • 39
  • 57
  • Check out [setOnClickListener](http://stackoverflow.com/questions/8615417/how-can-i-set-onclicklistener-on-arrayadapter), and for your case, [SimpleAdapter setOnClickListener](http://stackoverflow.com/questions/12661705/set-onclicklistener-in-list-view-with-image-and-text-using-simple-adapter). Hope it helped! – xosuma Mar 28 '16 at 19:49

2 Answers2

0

I think you are looking for OnItemClickListener of ListView.

Implement it something like this

listviewobject.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
  }
});
Arnold Laishram
  • 1,801
  • 2
  • 18
  • 25
0

first in your list_item.xml add id to each ImageView and lets suppose img1,img2,img3 in order extens SimpleAdapter and override getView as follows

 class CusAdapter extends android.widget.SimpleAdapter {

public CusAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view =  super.getView(position,convertView,parent);
    ImageView img1 = (ImageView)view.findViewById(R.id.img);
    img1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //do some thing
        }
    });
    ImageView img2 = (ImageView)view.findViewById(R.id.img);
    img2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do some thing
        }
    });
    ImageView img3 = (ImageView)view.findViewById(R.id.img);
    img3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do some thing
        }
    });
    return view;
}

}

then use CusAdapter

ListAdapter adapter = new CusAdapter(
        Resultat_recherche.this, contactList,
 R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});

setListAdapter(adapter);
Sam
  • 151
  • 7