Android 奇怪的光标行为
我使用listView显示数据库数据(3个字段,但仅显示名称),当我单击1个项目时,会显示一个AlertDialog,我可以看到该行的其他字段,我使用光标选择数据,然后只传递我想要的字符串,但每次按下一个,我都会得到一个不同的元素,我知道我可以对我想得到的行进行查询,但我对SQLite太差了,所以我宁愿这样做,下面是代码:Android 奇怪的光标行为,android,sqlite,Android,Sqlite,我使用listView显示数据库数据(3个字段,但仅显示名称),当我单击1个项目时,会显示一个AlertDialog,我可以看到该行的其他字段,我使用光标选择数据,然后只传递我想要的字符串,但每次按下一个,我都会得到一个不同的元素,我知道我可以对我想得到的行进行查询,但我对SQLite太差了,所以我宁愿这样做,下面是代码: listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Ov
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;
}
});
listView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
游标crs=db.rawQuery(“从Usuarios中选择*”,null);
crs.移动位置(位置);
//获取prompts.xml视图
LayoutInflater li=LayoutInflater.from(上下文);
视图提示视图1=li.充气(R.layout.dialog1,空);
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
上下文);
//将prompts.xml设置为alertdialog builder
alertDialogBuilder.setView(promptsView1);
最终文本视图nam=(文本视图)提示视图1
.findviewbyd(R.id.name1);
nam.setText(crs.getString(0));
最终文本视图ape=(文本视图)提示视图1
.findviewbyd(R.id.apellido2);
ape.setText(crs.getString(1));
最终文本视图位置=(文本视图)提示视图1
.findviewbyd(R.id.loc1);
loc.setText(crs.getString(2));
//设置对话框消息
alertDialogBuilder
.setCancelable(真);
//创建警报对话框
AlertDialog AlertDialog=alertDialogBuilder.create();
//表现出来
alertDialog.show();
//游标=空;
}
});
如果您使用此方法或查询语句提供任何帮助,我们将不胜感激。我不知道您想做什么。但我希望我能帮上忙 首先不要使用
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了,虽然它的类型很长,但不管怎样,这有什么意义?在调试器中,id和位置的值相同。项目的位置可以在query1和query2之间更改,其id
不会更改。