正如edwin所说,您可以制作一个Costum适配器.您可以在其中在每个View上使用setOnClickListner()方法.就像我在这里一样
class CustomAdapter extends ArrayAdapter
{
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomAdapter(Context context, int textViewResourceId,
ArrayList strings) {
//let android do the initializing :)
super(context, textViewResourceId, strings);
}
//class for caching the views in a row
private class ViewHolder
{
TextView id,name,phn_no;
}
ViewHolder viewHolder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
//inflate the custom layout
convertView=inflater.inflate(R.layout.activity_main, null);
viewHolder=new ViewHolder();
//cache the views
viewHolder.id=(TextView) convertView.findViewById(R.id.contact_id_txt);
viewHolder.name=(TextView) convertView.findViewById(R.id.contact_name_txt);
viewHolder.phn_no=(TextView) convertView.findViewById(R.id.contact_ph_no_txt);
viewHolder.id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hi!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.phn_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Heya!!!", Toast.LENGTH_SHORT).show();
}
});
//link the cached views to the convertview
convertView.setTag(viewHolder);
}
else
viewHolder=(ViewHolder) convertView.getTag();
//set the data to be displayed
viewHolder.id.setText(contacts.get(position).get_id()+"");
viewHolder.name.setText(contacts.get(position).get_name());
viewHolder.phn_no.setText(contacts.get(position).get_phn_no());
//return the view to be displayed
return convertView;
}
}