這是我的問題的解釋。
應用說明:我希望應用根據用戶點擊的按鈕顯示不同內容的列表視圖。爲此,我的數據庫中有一個帶有數字(1或2)的列,我想單擊按鈕1以僅顯示此數字爲1的元素,依此類推。SQL:Query()其中COL = int而不是字符串? (圖片+裏面的代碼)
A.當上的按鈕(稱爲按鈕1)用戶點擊,它會打開一個新的活動(ResultListViewActivity),併發送名爲「MYVARIABLE」通過putExtra整數。
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;
}
我希望此方法返回ac ursor,其中COL_NAME的內容(列名)和COL_COMMENTS的內容爲COL_CAT1 = myVariable(即,如果我點擊button1例如爲1)的每一行。
我試圖把我的int變成一個字符串使用Integer.toString(myVariable),但它仍然無法正常工作。此外,在我的活動之上,我定義COL_CAT1如下:
public static final String COL_CAT1 = "cat1";
我真的希望這是不夠清楚,有人會幫我做這個工作,我真的開始感到絕望這個東西...
提前致謝!
編輯:發佈代碼,而不是圖片
你一定要明白,沒有人願意重新輸入你的代碼,並通過使用而不是文本的截圖,你幾乎迫使他們這樣做,是嗎? – Marc 2013-03-05 03:53:21
對不起,編輯我的帖子。 – Phalanx 2013-03-05 03:59:47
@Phalanx使用COL_CAT1 +「=?」而不是COL_CAT1 = myVariable – 2013-03-05 04:08:20