奇怪的游标行为

时间:2016-02-13 16:27:11

标签: android sqlite

我使用listView显示DataBase数据(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;

            }
        });

使用此方法或一个(查询句子)的任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我不知道你想做什么。但我希望我能提供帮助。

首先,当您只需要一行时,请不要使用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)));