1

これが私の問題の説明です。
アプリの説明:ユーザーがクリックしたボタンに応じて、異なるコンテンツのリストビューをアプリケーションに表示させたい。これを行うには、データベースに番号(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);

    }

};

B. ResultListViewActivityには、この整数を入力として使用するメソッド(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_CAT1 = myVariable(たとえば、button1をクリックした場合は1)のデータベースのすべての行にCOL_NAME(列名)とCOL_COMMENTSの内容が含まれるカーソルを返すようにします。

Integer.toString(myVariable)を使用してintを文字列に変換しようとしましたが、それでも機能しません。また、私のアクティビティに加えて、COL_CAT1を次のように定義しました。

    public static final String COL_CAT1 = "cat1";

私はそれが十分に明確であり、誰かが私がこの仕事をするのを手伝ってくれることを本当に望んでいます、私は本当にこのことで必死に感じ始めます...

前もって感謝します !

編集:写真の代わりに投稿されたコード

4

1 に答える 1

1

クエリを次のように変更します:

 c=mydatabase.query(
            DATABASE_TABLE,
            new String[] { COL_NAME,COL_COMMENTS },
            COL_CAT1 + "=?" ,
            new String[] { Integer.toString(myVariable) }, null, null, null
        );
于 2013-03-05T03:58:57.990 に答える