我有一个由数据库填充的列表,我正在运行OnItemClick()方法。我会从被点击的项目中获取文本的内容是什么?
public void DisplayRecord(Cursor c) {
Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
switch (position) {
case 0:
//method to getText()?
break;
}
}
logcat的:
02-03 22:34:11.174: E/AndroidRuntime(1561): FATAL EXCEPTION: main
02-03 22:34:11.174: E/AndroidRuntime(1561): java.lang.IllegalStateException: database not open
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1312)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1391)
02-03 22:34:11.174: E/AndroidRuntime(1561): at com.Logik.dawn.of.ages.DBAdapter.getAllRecords(DBAdapter.java:87)
02-03 22:34:11.174: E/AndroidRuntime(1561): at com.Logik.dawn.of.ages.Inventory.onListItemClick(Inventory.java:130)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.app.ListActivity$2.onItemClick(ListActivity.java:342)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.widget.ListView.performItemClick(ListView.java:3561)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.os.Handler.handleCallback(Handler.java:587)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.os.Looper.loop(Looper.java:143)
02-03 22:34:11.174: E/AndroidRuntime(1561): at android.app.ActivityThread.main(ActivityThread.java:4293)
02-03 22:34:11.174: E/AndroidRuntime(1561): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 22:34:11.174: E/AndroidRuntime(1561): at java.lang.reflect.Method.invoke(Method.java:507)
02-03 22:34:11.174: E/AndroidRuntime(1561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 22:34:11.174: E/AndroidRuntime(1561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 22:34:11.174: E/AndroidRuntime(1561): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
Cursor c = this.getContentResolver().query(DBAdapter.getAllRecords(),null,null,null,null);
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
c.moveToPosition(position);
String s = c.getString(c.getColumnIndex(DBAdapter.KEY_ITEM));
}
答案 1 :(得分:1)
你可以使用你的数组;
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
String fromtext = from[position];
}
答案 2 :(得分:1)
此代码用于将日期从数据库添加到a,b(ArrayList),这里是一个Float Arraylist,这里的b是整数数组列表
public void data()
{
mySQLiteAdapter = new SQLiteAdapter(getActivity());
mySQLiteAdapter.openToRead();
Cursor c = mySQLiteAdapter.queueAll("TABLE NAME",COLUMN NAME,CONDITION);
//************************************************add date from database
if(c.moveToFirst())
do {
a.add(c.getString(0));// here a is Float Arraylist, 0 represent first column
b.add(c.getInt(1));// here b is Integer Array list, 1 represent second column
}
while (c.moveToNext());
}
和'variable.get(position)'用于从数组列表中获取信息
public void onListItemClick(ListView l, View view, int position, long id) {
switch (position) {
case 0:
//method to getText()?
xxx.SetText(a.get(position));
break;
}