Search code examples
javaandroidandroid-listviewandroid-listfragment

How to distinguish between views in a ListFragment with CustomArrayAdapter?


I would like to know if I can add logic\distinguish between views in my onListItemClick ? I have a listFragment with several views in it and would like to perform different operations. I have attempted to use the .isPressed() Method on my views with no success.

What is the correct way to do this. -Thank You.


Per the Advice of the marked answer I implemented the code below in my CustomArrayAdapter and all is working well. I have a ListFragment that implements onListItemClick for the cell AND clickable imagesViews within the same cell. Hope this helps the next person.

public class ZAdapter extends ArrayAdapter<HashMap<String, String>>{

protected static final String TAG = "myLog";
Context context;
ArrayList<HashMap<String,String>> myListOfMaps;

public ZAdapter(Context context, int textViewResourceId,    ArrayList<HashMap<String, String>> myListOfMaps) {
    super(context, textViewResourceId, myListOfMaps);
    // TODO Auto-generated constructor stub
    this.context=context;

    this.myListOfMaps = myListOfMaps;

}


public class MyViewHolder
{

    ImageView editImage;
    TextView tvTitle;
    TextView tvSubTitle;
    TextView tvRepEmail;
    TextView tvRepCell;

    public MyViewHolder(View v)
    {
        editImage = (ImageView) v.findViewById(R.id.row_icon);
         tvTitle = (TextView) v.findViewById(R.id.row_title);
         tvSubTitle = (TextView) v.findViewById(R.id.row_subTitle);
         tvRepEmail = (TextView) v.findViewById(R.id.tvRepEmail);
         tvRepCell = (TextView) v.findViewById(R.id.tvRepCell);
    }


}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final int index=position;

    View row = convertView;
    MyViewHolder holder = null;


    if(row==null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.row, parent, false);
    holder = new MyViewHolder(row);


    row.setTag(holder);

    }else{

        holder = (MyViewHolder) row.getTag();



    }



    holder.editImage.setImageResource(R.drawable.edit_img);holder.editImage.setTag("goEdit");
    holder.tvTitle.setText(myListOfMaps.get(position).get("prod"));
    holder.tvSubTitle.setText(myListOfMaps.get(position).get("dist"));
    holder.tvRepEmail.setText(myListOfMaps.get(position).get("repEmail"));
    holder.tvRepCell.setText(myListOfMaps.get(position).get("repPhone"));

    holder.editImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d(TAG, "*****************  getView() *********called"+index);

        }
    });


    return row;
}

}

-Thank You


Solution

  • Not sure you can handle it in onListItemClick function. One place you can handle event clicks is in Adapter class - getView function. In this function, you can find your controls and register event handlers.

    Note that, you only set event handlers in this block

    if (convertView == null) {
    
        // Find your controls and register event handlers here  
    }