2013-02-04 52 views
0

我有一個由數據庫填充的列表,我正在運行OnItemClick()方法。我會打電話來從被點擊的項目中獲取文本?由數據庫創建的列表視圖中的getText()

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) 

回答

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)); 

} 
+0

這給了我一堆的錯誤,並說數據庫沒有打開,查詢有錯誤, –

+0

@CoreyWilliams:你可以發佈你的logcat錯誤嗎? – Deepzz

+0

更新的原貼 –

1

您可以使用您的陣列,像;

@Override 
public void onListItemClick(ListView l, View view, int position, long id) { 
    String fromtext = from[position]; 
} 
+0

我不認爲這是正確的。 'from'數組包含一個鍵名。 –

1

這從數據庫到A,B(ArrayList的)碼爲附加日期,,這裏被浮動的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()); 
     } 

和「可變.get(position)'從Array列表中獲取infrormation

public void onListItemClick(ListView l, View view, int position, long id) { 
    switch (position) { 
     case 0: 
    //method to getText()? 
     xxx.SetText(a.get(position)); 
    break; 
} 
相關問題