SQL:Query()其中COL = int而不是string? (里面的图片+代码)

时间:2013-03-05 03:51:11

标签: android sql database integer

以下是我的问题的解释 应用说明:我希望应用程序根据用户点击的按钮显示包含不同内容的列表视图。为此,我的数据库中有一个带有数字(1或2)的列,我希望单击按钮1仅显示此数字为1的元素,依此类推。

A。当用户点击按钮(名为button1)时,它会打开一个新活动(ResultListViewActivity)并通过putExtra发送一个名为“myVariable”的整数。

private OnClickListener listener_button1 = new OnClickListener() {
    public void onClick(View v) {
        Intent t = new Intent(MainActivity.this,
                ResultListViewActivity.class);
        t.putExtra("myVariable", 1);
        startActivity(t);

    }

};
ResultListViewActivity中的

B。,有一个方法(findNameInTable),它使用这个整数作为输入:

private void displayListView() {

    Bundle bundle = getIntent().getExtras();
    int myVariable = bundle.getInt("myVariable");

    Cursor c = dbHelper.findNameInTable(myVariable);
    // the desired columns to be bound
    String[] columns = new String[] { DatabaseAdapter.COL_NAME,
            DatabaseAdapter.COL_COMMENTS, };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.name, R.id.comments, };
    // create the adapter using the cursor pointing to the desired data
    // as well as the layout information
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
            columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.listview);
    // Assign adapter to ListView
    listView.setAdapter(cursorAdapter);
}

C。此方法findNameInTable()在我的DatabaseAdapter类中定义如下:

    public Cursor findNameInTable(int myVariable) {
    c = myDatabase
            .query(DATABASE_TABLE, new String[] { COL_NAME , COL_COMMENTS }, COL_CAT1 = myVariable,
                    new String[] { Integer.toString(myVariable) }, null,
                    null, null);
    return c;
} 

我希望这个方法返回一个游标,其中COL_NAME(列名)和COL_COMMENTS的内容为COL_CAT1 = myVariable的数据库的每一行(例如,如果我点击button1,则为1)。

我尝试使用Integer.toString(myVariable)将我的int转换为字符串,但它仍然无效。另外,在我的活动之上,我按如下方式定义了COL_CAT1:

    public static final String COL_CAT1 = "cat1";

我真的希望它足够清楚,有人会帮助我完成这项工作,我真的开始对这件事感到绝望......

提前致谢!

编辑:发布代码而不是图片

1 个答案:

答案 0 :(得分:1)

将您的查询更改为:

 c=mydatabase.query(
            DATABASE_TABLE,
            new String[] { COL_NAME,COL_COMMENTS },
            COL_CAT1 + "=?" ,
            new String[] { Integer.toString(myVariable) }, null, null, null
        );
相关问题