I had created a custom list view. i added a list of contact details using custom adapter by subclassing the arrayadapter. if i select the particular contact in the list means i need to get that selected details. how can i achieve this. here my coding,
public class ContactListAdapter extends ArrayAdapter<ContactList> {
Context context;
int layoutResourceId;
ContactList objects[] = null;
View row;
public ContactListAdapter(Context context, int layoutResourceId, ContactList[] objects) {
super(context, layoutResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.layoutResourceId = layoutResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
row = convertView;
final ContactListHolder holder;
if ( row == null ) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactListHolder();
holder.image = (ImageView) row.findViewById(R.id.contactImage);
holder.name = (TextView) row.findViewById(R.id.contactName);
holder.number = (TextView) row.findViewById(R.id.contactNumber);
holder.check = (CheckBox) row.findViewById(R.id.selectedContact);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
row.setTag(holder);
holder.check.setTag(objects[position]);
} else {
holder = (ContactListHolder) row.getTag();
holder.check.setTag(objects[position]);
}
ContactList contact = objects[position];
if(contact.imageIcon != null) {
Bitmap imgBitmap = BitmapFactory.decodeByteArray(contact.imageIcon, 0, contact.imageIcon.length);
holder.image.setImageBitmap(imgBitmap);
} else {
holder.image.setImageResource(R.drawable.ic_launcher);
}
holder.name.setText(contact.name);
holder.number.setText(contact.number);
holder.check.setChecked(objects[position].isSelected());
return row;
}
static class ContactListHolder {
ImageView image;
TextView name;
TextView number;
CheckBox check;
}
}
in the manin activity i used the list view as,
ContactList contactList[] = new ContactList[MyTrackList.size()];
for(int i=0;i<MyTrackList.size();i++) {
MyContact contact = MyTrackList.get(i);
contactList[i] = new ContactList(contact.getName(), contact.getNumber(), contact.getImage());
}
ContactListAdapter adapter = new ContactListAdapter(this, R.layout.manage_track_list_custom_view, contactList);
trackList = (ListView) findViewById(R.id.manage_track_listView);
trackList.setAdapter(adapter);
here Contact list is a class which has many objects.
i tried by this way but its not work out. please guide me. thanks in advance.
set setOnItemClickListener
or setOnItemSelectedListener
to list and you will get the call on item clicked and selected...
setOnItemClickListener
Register a callback to be invoked when an item in this AdapterView has been clicked.
trackList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
contact = contactList[position]
}
});
..
setOnItemSelectedListener
Register a callback to be invoked when an item in this AdapterView has been selected.
trackList.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
// TODO Auto-generated method stub
contact = contactList[position]
// or
// Object obj= parent.getItemAtPsotion(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
First I would define an onclick listener in an XML layout for displaying the list items. This is done by adding the attribute to the top-level layout view.到顶级布局视图来完成的。
Then in your main activity you need to implement the method defined in your XML layout (ie methodName( View v )), which will get called when a user taps on a list item.
Try this,
eg:
ListView lv = (ListView)findViewById(R.id.myList);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
// Get the item here
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.