从数据库ListView中删除项目

时间:2014-02-22 20:36:26

标签: java android sqlite android-listview

我的Android应用程序遇到了问题。我只能删除ListView中的项目,但不能删除我的数据库中的项目。我需要一些帮助。我需要从我的数据库中删除它。这是我的代码:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, strlist2);
    listContent.setAdapter(adapter);
    listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listContent.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listContent.setSelector(android.R.color.holo_blue_light);
            listContent.setItemsCanFocus(false);
            final int positionToRemove = position;
            String string = String.valueOf(id);
            dbAdapter.remove(string);
            dbAdapter.delete123456(string);
            adapter.remove(adapter.getItem(positionToRemove));
            adapter.notifyDataSetChanged();
        }
    });

这是我的dbAdapter.remove和我的dbAdapter.delete123456代码:

    public boolean delete123456(String rowId) {
/*      this is what your database delete method should look like
    this method deletes by id, the first column in your database*/
    return mDb.delete(FTS_VIRTUAL_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public void remove(String id){
    mDb.delete(FTS_VIRTUAL_TABLE, KEY_ROWID +"=?", new String [] { id });
}

我需要帮助。我一整天都在墙上敲头。关于这个问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这种方式。

listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listContent.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listContent.setSelector(android.R.color.holo_blue_light);
            listContent.setItemsCanFocus(false);
            String[] delete = names2.get(position);
            String idString = delete[0];
            long idLong = Long.valueOf(idString);
            Log.d("Deleting...", idLong + "");
            dm.delete(idLong);        // dm is your Datamanipulator class object
            names2.remove(position);
            stg1=new String[names2.size()];
            int x=0;
            String stg;

            for (String[] dag : names2) {
                stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
                stg1[x]=stg;
                x++;
            }
           adapter.notifyDataSetChanged();
        }
    });

此处 names2 是您的字符串列表数组,因此请将其替换为您的数组名称。