问题描述
- 处理 ListView 中一个 list item的问题
-
我创建了一个自定义的Listview。List中的每一个item都有一个 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();
我在toast 中获取第一个item的信息:
"selected item: Mary"
如何才能获得接下来以后的信息呢?
时间: 2024-09-21 21:33:11