SQLiteデータベースからデータをロードするlistViewがあり、今はリストにonclicklistenerを実装したいと思います。ユーザーがリストをクリックすると、次のアクティビティに移動し、対応するデータをTextViewにロードする必要があります。私の質問は、リストのデータをどのように渡すかです。たとえば、「トピック」のリストであり、ユーザーがトピック「マイホーム」をクリックします。トピック「マイホーム」を次のアクティビティに渡して、それぞれ取得する対応するデータを確認したいと思います。
どうすればいいですか?新しいインテントに「putExtras」を追加しますか?または別の方法があります。以下は、リストビューを表示する私のコードの一部です。
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
助けてください。
ありがとうございました。