(首先,我很抱歉我的英文不好,请修理一下)
嗨,我可以从数据库中填充listview,如果我点击任何listview项目,它会通过相关的“id”转到“Detail.class”。
问题是当我重新填充课程(lesson =“WHERE deck ='”+ lesson +“'”;)时,listview正确填充但listview index / id?不是更改,当我单击listview项目时,“Detail.class”正在打开,但错误的ID。
-----数据库助手类---
if (lesson.equals("")) {
lesson= "";
} else {
lesson= " WHERE deck='" + MainActivity.lesson + "'";
}
String selectQuery = "select word, id,word,typ from TAB" + lesson + " group by id";
---- MainActivity类----
private void loadListview() {
Database db = new Database(getApplicationContext());
List<String> labels = db.makeList();
dataAdapter = new ArrayAdapter<String>(this, R.layout.listview_spc, labels);
Listview1 = (ListView) findViewById(R.id.liAll);
Listview1.setAdapter(dataAdapter);
}
Listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(getApplicationContext(), Detail.class);
intent.putExtra("id", (int) word_ids[arg2]); // I think the problem is here.
startActivity(intent);
}
});
答案 0 :(得分:0)
将clicklistner代码替换为以下内容,
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), Detail.class);
intent.putExtra("Id",labels.get(position).getId());
startActivity(intent);
}
});