Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get view for an item in listview in android?

Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?

I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.

My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.

like image 596
Kalimah Avatar asked Jan 01 '12 13:01

Kalimah


3 Answers

Here's how I accomplished this. Within my (custom) adapter class:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = null;
  if (convertView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    view = inflater.inflate(textViewResourceId, parent, false);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.name = (TextView) view.findViewById(R.id.name);
    viewHolder.button = (ImageButton) view.findViewById(R.id.button);

    viewHolder.button.setOnClickListener
      (new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        int position = (int) viewHolder.button.getTag();
        Log.d(TAG, "Position is: " +position);
      }
    });

    view.setTag(viewHolder);
    viewHolder.button.setTag(items.get(position));

  } else {
    view = convertView;
    ((ViewHolder) view.getTag()).button.setTag(items.get(position));
  }


  ViewHolder holder = (ViewHolder) view.getTag();

  return view;
}

Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.

Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.

like image 194
Marvin Pinto Avatar answered Oct 19 '22 00:10

Marvin Pinto


See below code:

 public static class ViewHolder
{
    public TextView nm;
    public TextView tnm;
    public TextView tr;
    public TextView re;
    public TextView membercount;
    public TextView membernm;
    public TextView email;
    public TextView phone;
    public ImageView ii;
}
class ImageAdapter extends ArrayAdapter<CoordinatorData> 
{
    private ArrayList<CoordinatorData> items;
    public FoodDriveImageLoader imageLoader;
    public ImageAdapter(Context context, int textViewResourceId,ArrayList<CoordinatorData> items) 
    {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        ViewHolder holder = null;
        if (v == null) 
        {
            try
            {
                holder=new ViewHolder();
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                imageLoader =   new FoodDriveImageLoader(FoodDriveModule.this); 
                v = vi.inflate(R.layout.virtual_food_drive_row, null);
                //System.out.println("layout is null.......");
                holder.nm = (TextView) v.findViewById(R.id.name);
                holder.tnm = (TextView) v.findViewById(R.id.teamname);
                holder.tr = (TextView) v.findViewById(R.id.target);
                holder.re = (TextView) v.findViewById(R.id.received);
                holder.membercount = new TextView(FoodDriveModule.this);
                holder.membernm = new TextView(FoodDriveModule.this);
                holder.email = new TextView(FoodDriveModule.this);
                holder.phone = new TextView(FoodDriveModule.this);
                holder.ii = (ImageView) v.findViewById(R.id.icon);

                v.setTag(holder);
            }
            catch(Exception e)
            {
                System.out.println("Excption Caught"+e);
            }
        }
        else
        {
            holder=(ViewHolder)v.getTag();
        }
        CoordinatorData co = items.get(position);
        holder.nm.setText(co.getName());
        holder.tnm.setText(co.getTeamName());
        holder.tr.setText(co.getTarget());
        holder.re.setText(co.getReceived());
        holder.ii.setTag(co.getImage());

        imageLoader.DisplayImage(co.getImage(), FoodDriveModule.this , holder.ii);

        if (co != null) 
        {
        }
        return v;
    }

}
like image 3
Shreyash Mahajan Avatar answered Oct 18 '22 23:10

Shreyash Mahajan


A better option is to identify using the data returned by the CursorAdapter rather than visible views. For example if your data is in a Array , each data item has a unique index.

like image 1
Rajdeep Dua Avatar answered Oct 18 '22 23:10

Rajdeep Dua