且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从列表中,并从数据库中删除Android项目?

更新时间:2022-04-18 22:06:30

您或许应该从的 SimpleCursorAdapter 类,而不是你目前在做什么题库字符串的对象。这例如,应该让你开始把你的数据插入适配器。

You should probably extend from the SimpleCursorAdapter class instead of what you're currently doing to only pass in String objects. This example, should get you started on putting your data into the adapter.

不过,如果你想只修改你目前在做什么,我会建议如下。除了你的的ArrayList<串GT;结果我想创建一个额外的对象的ArrayList<整数GT; resultsIds ,将持有的ID为每一个项目。

However, if you want to only modify what you're currently doing, I would suggest the following. In addition to your ArrayList<String> results I would create an additional object ArrayList<Integer> resultsIds that would hold the ids for each item.

results.add("" + id + ", " + persName + ", " + persSurname);
resultsIds.add(id);

然后在你的点击监听器做到以下几点:

Then in your click listener do the following:

 @Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
    super.onListItemClick(listView, view, position, id);
    int id = resultsIds.get(position); // this is the id for the item in the database you want to delete
    // now with your myDataBase SqliteDatabse object delete the item from the databse.
    // then remove it from the list
    ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
    adapter.remove( results.get(position) );
    // and you'll probably now want to remove the String and id from your results
    results.remove(position);
    resultsIds.remove(position);
}