【问题标题】:How to get SQLite database Column values as List in android如何在android中获取SQLite数据库列值作为列表
【发布时间】:2013-12-17 08:36:23
【问题描述】:

我有三列,第一个 id,第二个标题和第三个正文。

这样

id         title         body
1           t1           b1
2           t2           b2
3           t3           b3

所以,我想用 listview 获取标题并显示在列表中,如果我点击 t2 标题,则显示相应可点击标题的正文。

我做了什么。

DatabaseHelper.java

public Cursor retriveTitle(String [] title) {
        Cursor c = null;
        c = myDataBase.rawQuery(
                "select id,title from book,", title);
        return c;
    }

Main.java

private void displayTitle() {
        try {
            myDataBase = (new DatabaseHelper(this)).getWritableDatabase();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Cursor cursor = myDataBase.rawQuery("SELECT b.id, b.title FROM book b  ",   new String[] { ""  });

        String test=cursor.getString(cursor
                .getColumnIndex("title"));
        //How to display title of book in listview
    }

得到这个:

01-18 14:20:03.401: E/AndroidRuntime(6704): Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.

根据这篇博文,我正在使用数据库文件:Using your own SQLite database in Android applications

【问题讨论】:

    标签: android listview android-sqlite


    【解决方案1】:

    试试

    Cursor cursor = myDataBase.rawQuery("SELECT b.id, b.title FROM book b",null);
       curosr.moveToFirst();
        String test=cursor.getString(cursor
                .getColumnIndex("title"));
    

    【讨论】:

      【解决方案2】:

      我认为您的查询应该使用“where”语句

      c = myDataBase.rawQuery("select id,title from book where title=?", title);
      

      当然要传递一定数量的参数

      c = myDataBase.rawQuery("select id,title from book where title in (?, ?, ?)", new String[]{title1, title2, title3});
      

      但是如果标题的数量可以变化,您将需要动态创建一个带有足够问号的字符串。看这篇文章

      IN clause and placeholders

      如果您仍然遇到错误,也许提供更多日志会有所帮助。此外,如果 lo 说哪条线路失败,请告诉它是哪条线路。我们在这里看不到行号

      祝你好运

      【讨论】:

        【解决方案3】:

        这对我来说是这样的。在另一个类中,我创建了一个 List 类型的方法,并在 sqlite 数据库的列表中添加了列 ID 和项目的名称。

        这是检索所选项目的列 id 的一段代码。

            public List<String> list;
            adapt = new MyAdapter(this, R.layout.list_inner_view, list); //custom adapter
            listTask.setAdapter(adapt);
        
            listTask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int position, long l) {
        
               Integer id = list.get(position).getId();
               //position is the position of the item in the list.
              }
           });
        

        结果:该项目在列表中的第 3 位。它的数据库 column_id(自动增量)值是 12,因为我已经删除了以前的项目。所以你期望的结果是 12,也就是变量 id 的值。

        如果您不完全理解这一点,可能是因为我没有发布我的整个代码。但我会帮你解决你的问题。

        【讨论】:

          猜你喜欢
          • 2010-10-10
          • 2011-09-21
          • 2014-04-01
          • 2021-02-10
          • 2011-09-15
          • 1970-01-01
          • 1970-01-01
          • 2016-11-20
          • 1970-01-01
          相关资源
          最近更新 更多