1

我一直在寻找这个问题的答案,所以希望有人可以帮助我。

我有一段代码应该从 SQLite DB 创建的 ListView 中选择一个项目,将其返回到一个字符串,以便我可以运行 SQL 命令从 DB 中删除记录。但是,当单击条目时,它会从其下方的条目中返回数据,而不是选择的条目,因此我认为它选择了错误的索引。

如果有 3 个条目,比如 Adam、Bob 和 Charlie,如果我点击 Adam,它会将 Bob 返回到字符串;如果我点击 Bob,它将返回 Charlie;如果我点击 Charlie,它会抛出一个 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 函数主要工作。

谢谢

4

1 回答 1

0

代替

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

利用

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

返回的 ListAdaptergetListAdapter不知道您的标题视图,因此索引关闭了 1。

于 2013-01-16T22:46:36.993 回答