Android怎么获取ListView中被点击条目的值

2025-01-02 23:55:30
推荐回答(1个)
回答1:

可以做一个数据相对应的pojo类 列如Student.java 设置姓名、学号、成绩私有属性的set、get方法,重写toString方法

public class ListViewTest extends Activity{
private ListView lv;
private List stus;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
stus=new ArrayList();
stus.add(new Student(1001,"zhangsan",20,85));
stus.add(new Student(1002,"lisi",21,87.5));
stus.add(new Student(1003,"wangwu",22,88));
stus.add(new Student(1004,"zhaoliu",20,75));
stus.add(new Student(1005,"qianqi",21,97.5));
stus.add(new Student(1006,"liuba",22,68));

setContentView(R.layout.list_view_test);

lv=(ListView)findViewById(R.id.list_view_01);

List> data=new ArrayList>();
for(int i=0;iMap map=new HashMap();
map.put("s_id", stus.get(i).getId());
map.put("s_name", stus.get(i).getName());
map.put("s_age", stus.get(i).getAge());
map.put("s_score", stus.get(i).getScore());
data.add(map);
}

SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.list_view_item_02,new String[]{"s_id","s_name","s_age","s_score"},new int[]{R.id.s_id_text_2,R.id.s_name_text_2,R.id.s_age_text_2,R.id.s_score_text_2});
lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Intent it=new Intent(ListViewTest.this,StudentDescribeActivity.class);
it.putExtra("student", stus.get(arg2));