android怎样获得ListView上被选中的数据

2025-01-02 23:53:07
推荐回答(2个)
回答1:

setOnItemClickListener();监听被点击的item

回答2:

// 绑定数据生成列表
ArrayList> list = InitFileList(fileSavedPath);

SimpleAdapter listItemAdapter = new SimpleAdapter(this, list,
R.layout.listitem, new String[] { "name", "status" },
new int[] { R.id.textViewFilename, R.id.textViewStatus });

ListView lv = (ListView) findViewById(R.id.listViewFilelist);
lv.setAdapter(listItemAdapter);
lv.setOnItemClickListener(itemclick);
}

OnItemClickListener itemclick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
try {
String filename = fileList[arg2];
Intent intent = new Intent(ActFileList.this,
ActFileDownloader.class);
intent.putExtra("filename", filename);
intent.putExtra("serverip", serverip);
intent.putExtra("saveto", saveto);
startActivity(intent);
} catch (NullPointerException e) {
// 没有数据,不操作
}
}
};