1

I have the following layout as an item layout in my ListView. How to handle the first ImageView click which has iv_info as ID?

Do I have to use both onItemClick and onClick or anything else?

<?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="wrap_content"
    android:background="@drawable/bg_list_row_9patch"
    android:orientation="horizontal"
    android:padding="@dimen/fifteen_dp" >

    <ImageView
        android:id="@+id/iv_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_iv_weight"
        android:contentDescription="@string/help"
        android:src="@drawable/btn_info" />

    <TextView
        android:id="@+id/erl_tv_exsercise_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_tv_weight"
        android:paddingLeft="@dimen/ten_dp"
        android:textColor="@android:color/white"
        android:textSize="@dimen/list_row_text_size"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/erl_tv_participant_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_tv_weight"
        android:gravity="right"
        android:textColor="@android:color/white"
        android:textSize="@dimen/list_row_text_size" >
    </TextView>

    <ImageView
        android:id="@+id/iv_exercise_status"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_iv_weight"
        android:src="@drawable/btn_info" />

</LinearLayout>
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
  • the proper way is to use onItemClick and an onItemClickListener outside the adapter (as the click handling as nothing to do with the adapter.) – njzk2 Nov 23 '12 at 13:16
  • @njzk2, how to get a separate view? Give an example, please, if you have one. – Maksim Dmitriev Nov 23 '12 at 13:23
  • at the place where you put the adapter in the list view, add an onItemClickListener to your list view as well. – njzk2 Nov 23 '12 at 13:53

2 Answers2

3

in adapter's getView method, get the image view and set onClickListener..

Example:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText("item "+position);
    holder.image.setTag(data.get(position));
    holder.image.setOnClickListener(this);
    holder.text.setOnClickListener(this);
    imageLoader.DisplayImage(data.get(position), activity, holder.image);
    return vi;
}
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • I accepted and gotta another one. How to get item in this `onClick` method? I'm trying doing it now. – Maksim Dmitriev Nov 23 '12 at 13:21
  • you can set tag with the position like in the answer above..then get the position in the onClick.. – Nermeen Nov 23 '12 at 13:22
  • Thank to http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view I understood the purpose of `setView()` and `getView` methods – Maksim Dmitriev Nov 23 '12 at 14:39
1
    View vi=convertView;
    ViewHolder holder;

    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();       

        holder.iv_info=(ImageView)vi.findViewById(R.id.iv_info);
        holder.iv_info.setOnClickListener(activity);

        holder.iv_exercise_status=(ImageView)vi.findViewById(R.id.iv_exercise_status);
        holder.image3.setOnClickListener(activity);

        vi.setTag(holder);
    } else
    holder=(ViewHolder)vi.getTag();


    public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {
            case R.id.iv_info:
              // do it smth
              break;
            case R.id.iv_exercise_status:

              break;               
          }
     }
Talha
  • 12,673
  • 5
  • 49
  • 68