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..