我有一个从 SQLite 数据库加载数据的 listView,现在我想在列表中实现一个 onclicklistener。当用户点击列表时,它应该将他们带到下一个活动并将相应的数据加载到 TextView 中。我的问题是我将如何传递列表的数据,例如它是“主题”列表,用户单击“我的家”主题。我想将主题“我的家”传递给下一个活动,以便我知道分别要检索哪些相应的数据。
我该怎么做?我是否“将Extras”添加到新意图?或者还有另一种方法。以下是我显示列表视图的部分代码:
ListView listContent = (ListView) findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
int[] to = new int[] { R.id.text };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class);
// summaryIntent.putExtra("SummTopic", value);
}
});
已编辑:这部分是关于下一个活动的。
Intent i = getIntent();
extraTopic = i.getStringExtra("SummTopic");
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE + " WHERE topic = \" " + extraTopic + " \" " , null);
Integer cindex = allrows.getColumnIndex("topic");
Integer cindex1 = allrows.getColumnIndex("text1");
Integer cindex2 = allrows.getColumnIndex("text2");
从数据库检索时出现错误:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
请帮忙。
谢谢你。