请求索引2,大小为2

时间:2014-10-28 11:02:35

标签: android eclipse sqlite indexing size

我是android开发的新手,这是我的第一个gridview数据库项目。 它显示错误“索引2请求大小为2”

我正在使用sqlite数据库db文件

这是我的代码

请帮帮我!!!!!

public class MainActivity extends Activity {
SQLiteDatabase mydb;
GridView data;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = (GridView) findViewById(R.id.gridView1);
    List<String> li = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,
            li);
    dataAdapter.setDropDownViewResource(R.layout.activity_main);
    try {
        mydb = openOrCreateDatabase(getString(R.string._sdcard_sales_db), MODE_PRIVATE, null);
        Cursor cr = mydb.rawQuery("SELECT * FROM users", null);
        if (cr != null) {
            if (cr.moveToFirst()) {
                do {
                    String desc = cr.getString(cr.getColumnIndex("user"));
                    li.add(desc);
                } while (cr.moveToNext());
                Toast.makeText(getApplicationContext(), cr.getString(cr.getColumnIndex("user")),
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "no data",
                        Toast.LENGTH_LONG).show();

            }
        }
        cr.close();
        mydb.close();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "ERROR" + e.getMessage(),
                Toast.LENGTH_LONG).show();

    }

}}

提前致谢

1 个答案:

答案 0 :(得分:1)

do {
    String desc = cr.getString(cr.getColumnIndex("user"));
    li.add(desc);
} while (cr.moveToNext());
Toast.makeText(getApplicationContext(), cr.getString(cr.getColumnIndex("user")),
        Toast.LENGTH_LONG).show();

在do-while循环之后,光标cr指向最后一个有效行之后的一行。

删除使用无效游标索引调用Toast的{​​{1}},或将其更改为覆盖您实际需要的信息。