2013-01-16 40 views
1

我一直在尋找答案,希望有人能幫助我。Android從上下文菜單中選擇錯誤的數據

我有一段代碼應該從一個從SQLite數據庫創建的ListView中選擇一個項目,並將其返回給一個字符串,以便我可以運行SQL命令從數據庫中刪除記錄。然而,當點擊條目時,它會從下面的條目中返回數據而不是所選的條目,所以我相信它是在錯誤的索引上選擇的。

如果有3個條目,說亞當,鮑勃和查理,如果我點擊亞當,它會返回鮑勃的字符串;如果我點擊鮑勃,它會返回查理;如果我點擊查理它會拋出一個IndexOutOfBoundsException!

我的代碼如下:

public class viewdb extends ListActivity { 

    private static final int DELETE_ID = Menu.FIRST + 1; 
    private static final int EDIT_ID = Menu.FIRST + 2; 
    private ArrayList<String> results = new ArrayList<String>(); 
    private String tableName = DBAdapter.DATABASE_TABLE; 
    private SQLiteDatabase newDB; 
    String strfieldName; 
    String strSelFN; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d("CalledActivity", "OnCreate"); 
     Intent in = new Intent(); 
     openAndQueryDatabase(); 

     displayResultList(); 
     registerForContextMenu(getListView()); 

    } 

    public void displayResultList() { 
     TextView tView = new TextView(this); 
     tView.setText("Fields in Database"); 
     getListView().addHeaderView(tView); 

     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, results)); 
     getListView().setTextFilterEnabled(true); 

    } 

    public void openAndQueryDatabase() { 
     try { 
      DatabaseHelper dbHelper = new DatabaseHelper(
        this.getApplicationContext()); 
      newDB = dbHelper.getWritableDatabase(); 
      Cursor c = newDB.rawQuery("SELECT Field_Name FROM " + tableName, 
        null); 

      if (c != null) { 
       if (c.moveToFirst()) { 
        do { 
         strfieldName = c.getString(c.getColumnIndex("Field_Name")); 
         results.add(strfieldName); 
        } while (c.moveToNext()); 
       } 
      } 
     } catch (SQLiteException se) { 
      Log.e(getClass().getSimpleName(), 
        "Could not create or open the database"); 
      newDB.close(); 
     } 

    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Object o = this.getListAdapter().getItem(position); 
     strSelFN = o.toString(); 
     getListView().showContextMenu(); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("Entry Menu"); 
     menu.add(Menu.NONE, EDIT_ID, Menu.NONE, R.string.strEdit); 
     menu.add(Menu.NONE, DELETE_ID, Menu.NONE, R.string.strDelete);} 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 

     if (item.getItemId() == DELETE_ID) { 
      // newDB.execSQL("DELETE FROM " + tableName + 
      // " WHERE Field_Name = '" + strSelFN + "'"); 
      Toast.makeText(this, "Field Deleted: " + strSelFN, 
        Toast.LENGTH_LONG).show(); 
      return true; 
     } 
     return false; 

    } 

} 

我還沒有定義EDIT_ID但作爲需要DELETE_ID功能主要工作。

感謝

回答

0

而不是

Object o = this.getListAdapter().getItem(position); 

使用

Object o = this.getListView().getItemAtPosition(position); 

通過getListAdapter返回ListAdapter不知道你的頭視圖,因此該指數是關閉的1

+0

完美,謝謝dokkaebi! – CSUK

相關問題