我创建了一个自定义 ListView。列表中的每一项都有一个 imageView 和两个 TextView。
代码是:
public class PersonalList extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] name= new String[] { "Mary", "Frank",
"John" };
String[] surname = new String[] { "Ballak", "Doe",
"Strip"" };
setContentView(R.layout.member_list);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, name, surname);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String name = (String) getListAdapter().getItem(position);
Toast.makeText(this, "selected item: "+name, Toast.LENGTH_LONG).show();
}
}
其中 MySimpleArrayAdapter 是:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] name;
private final String[] surname;
public MySimpleArrayAdapter(Context context, String[] name, String[] surname)
{
super(context, R.layout.list_row, name);
this.context = context;
this.name= name;
this.surname = surname;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView nameView= (TextView) rowView.findViewById(R.id.label);
TextView surnameView= (TextView) rowView.findViewById(R.id.label1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
nameView.setText(name[position]);
surnameView.setText(surname[position]);
return rowView;
}
}
从以下代码调用onListItemClick
:
Toast.makeText(this, "selected item: "+name, Toast.LENGTH_LONG).show();
我为第一项获得了这样的敬酒消息:
"selected item: Mary"
我该怎么做才能获得以下结果?
"selected item: Mary Ballak"