奇怪的光标行为
问题描述:
即时通讯使用listview来显示一个数据库数据(3个字段,但只显示名称),当我点击1项目,AlertDialog出现,我可以看到该行的其他字段,我使用游标选择数据然后传递我想要的字符串,但是每当我按下一个时,我就会得到一个预期的不同元素,我知道我可以对我想获得的行进行查询,但是在SQLite中我很糟糕所以我宁愿做这种方式,这里是代码:奇怪的光标行为
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor crs = db.rawQuery("SELECT * FROM Usuarios", null);
crs.moveToPosition(position);
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView1 = li.inflate(R.layout.dialog1, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView1);
final TextView nam = (TextView) promptsView1
.findViewById(R.id.name1);
nam.setText(crs.getString(0));
final TextView ape = (TextView) promptsView1
.findViewById(R.id.apellido2);
ape.setText(crs.getString(1));
final TextView loc = (TextView) promptsView1
.findViewById(R.id.loc1);
loc.setText(crs.getString(2));
// set dialog message
alertDialogBuilder
.setCancelable(true);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
//cursor = null;
}
});
使用这种方法或一个(查询语句)任何帮助,将不胜感激
答
我不知道你想做什么。但我希望我能帮上忙。
首先,当您只需要一行时,请不要使用select *
,而应使用ID等密钥列。
cursor = db.rawQuery("Select * from " + TABLE + " where " + where, null);
那么,如果查询返回一行,将光标移动到第一位置和读取数据
if (cursor != null)
if (cursor.getCount() != 0)
cursor.moveToFirst();
item.setText(cursor.getString(cursor
.getColumnIndex(ClassesConst.COLUMN_TEXT)));
不要使用'rawQuery',不使用'position'但使用'id'。这一定会打破 –
我应该用什么来代替rawQuery?我已经尝试使用ID,尽管它的长型,反正有什么意义?在调试器中,Im为id和位置获取相同的值 – Aleeeeeeeeeeeeex
项目的位置可以在query1和query2之间变化,其'id'不会。 –