在父级单击的可展开列表中,我有一个子级,其中有3个文本视图,例如,当我单击不同的文本视图时,我希望触发不同的事件。
for text view 1 output is hello
for text view 2 output is hi
for text view 3 output is heya
最佳答案
正如埃德温所说,你可以做一个CoStum适配器。其中可以对每个视图设置onclicklistner()方法。就像我在这里一样…
class CustomAdapter extends ArrayAdapter<Contact>
{
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<Contact> 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;
}
}
关于android - 如何在android中的可扩展 ListView 中触发子点击部分上的事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14521957/