0

I have a gridview that populates a contact's information (an image, name and phonenumber) by using custom adapter. On the click of a particular item, i want to retrieve the contact's information(info of image, name and phonenumber). I know i have to use the setTag() and getTag() but i can't figure it out how to do this. Here is my code..

getView method of custom adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ContactViewHolder contactViewHolder;
    if (convertView == null) {
        LayoutInflater li = getLayoutInflater();
        convertView = li.inflate(R.layout.activity_grid_one_item, null);
        contactViewHolder = new ContactViewHolder();
        contactViewHolder.imgContact = (ImageView) convertView
                .findViewById(R.id.imgViewContactImage);
        contactViewHolder.txtViewContactName = (TextView) convertView
                .findViewById(R.id.txtViewContactName);
        contactViewHolder.txtViewPhoneNumber = (TextView) convertView
                .findViewById(R.id.txtViewPhoneNumber);
        convertView.setTag(contactViewHolder);
    } else {
        contactViewHolder = (ContactViewHolder) convertView.getTag();
    }
    curObj.moveToPosition(position);
    String name = curObj.getString(curObj.getColumnIndex("name"));
    if (name != null) 
        contactViewHolder.txtViewContactName.setText(name);
    else 
        contactViewHolder.txtViewContactName.setText("Unknown");
    String phoneNumber = curObj.getString(curObj.getColumnIndex("number"));

    if (phoneNumber != null)
        contactViewHolder.txtViewPhoneNumber.setText(phoneNumber);
    else 
        contactViewHolder.txtViewPhoneNumber.setText("Unknown");

            String image = curObj.getString(curObj.getColumnIndex("image"));

    if(image.equalsIgnoreCase("R.drawable.addcontactsmall2"))
            contactViewHolder.imgContact.setImageResource(R.drawable.addcontactsmall2);
    else
    {
        Uri uri = Uri.parse(image);
        contactViewHolder.imgContact.setImageURI(uri);
    }
    return convertView;
}

public class ContactViewHolder {
    ImageView imgContact;
    TextView txtViewContactName;
    TextView txtViewPhoneNumber;
}

onItemClick method

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    View view = (View) arg1.getTag(arg2);
    TextView txtPhoneNumber = (TextView) view.findViewById(R.id.txtview_addcontact_phonenumber);
    System.out.println(txtPhoneNumber.getText().toString());
}

i know the above code doesn't work, (and it didn't work as well, as expected i get a null pointer exception), so if anyone can guide me on how to do this, it would be of much help..

jimpanzer
  • 3,470
  • 4
  • 44
  • 84
Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149

2 Answers2

0

Change this in onItemClick

ContactViewHolder view = (ContactViewHolder ) arg1.getTag();
String number = view.getPhNumber();

and add this in ViewHolder

public class ContactViewHolder {
   ImageView imgContact;
   TextView txtViewContactName;
   TextView txtViewPhoneNumber;

   String phNumber;
   public String getPhNumber() {
       return phNumber;
   }
}

add this in getView()

String phoneNumber = curObj.getString(curObj.getColumnIndex("number"));
phNumber = phoneNumber ;

add same code for remaining params. it will work.

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Thanks a lot it worked... Just a small change in phNumber = phoneNumber, changed to contactViewHolder.phNumber = phonenumber; – Vamsi Challa Apr 23 '13 at 12:41
  • i think its not a good way to handle to this task.its would complicated when multiple parms & arrays are holding that values – Chintan Khetiya Apr 23 '13 at 12:44
0

you can get custom grid item in setOnItemClickListener as well as setOnClickListener.

public ImageView img;

your_grid.setOnItemClickListener(new OnItemClickListener()
  {
       public void onItemClick(AdapterView<?> parent, View v, final int position, long id)
          {
               img = (ImageView)v.findViewById(R.id.grid_item_image);
           }
  });
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85