如何从列表中,并从数据库中删除Android项目? [英] How to delete item from list and from database in android?

查看:174
本文介绍了如何从列表中,并从数据库中删除Android项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多有关如何从列表中删除项目,但我无法找到一个我期待的问题。结果
所以基本上,我有一个列表和DB,我怎么从两个列表和DB删除?结果
因为我的名单得到从填充的ArrayList<串GT; 这样一个项目简直是字符串。比如我在我的名单就像两个项目 - 1.John,能源部2.Jane,能源部结果
我如何获得这些项目的id?结果
如果我用这个code,并通过点击物品检查ID:

There are a lot of questions about how to delete item from list, but I couldn't find the one I am looking for.
So basically, I have a list and DB, how do I delete from both list and DB?
Because my list get populated from ArrayList<String> so one item is simply string. For example I have two items in my list like - 1.John,Doe 2.Jane,Doe
How do I get those items ids?
If I use this code and check id by clicking on items:

listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getApplicationContext(),
    Long.toString(parent.getItemIdAtPosition(position)) , 
    Toast.LENGTH_SHORT).show();  }});

它显示0或1,但不是1或2(这是相当合乎逻辑的)结果
所以啊,我怎么得到这些物品的ID?我现在能想到的唯一的选择就是让我的列表项的头两个或三个字符,并将其转换为int,并传递给我的SQL语句,然后在那里我删除DB相应的物品。结果
也许有人有更好的想法?

It shows 0 or 1 but not 1 or 2.(which is quite logical)
So yea, how do I get those items ids? The only option I can think now is to get my list item's first two or three chars and convert them to int and pass to my SQL statement where then I delete corresponding items from DB.
Maybe someone has any better ideas?

编辑:

private void getAllData()
{
    cursor = myDataBase.rawQuery("SELECT * FROM " +
            MY_TABLE, null);

    if (cursor != null)
    {
        if (cursor.moveToFirst())
        {
            do
            {
                int id = cursor.getInt(cursor.getColumnIndex("Id"));
                String persName = cursor.getString(cursor.getColumnIndex("Name"));
                String persSurname= cursor.getString(cursor.getColumnIndex("Surname"));
                results.add("" + id + ", " + persName + ", " + persSurname);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}

结果私人列表&LT;串GT;结果=新的ArrayList&LT;串GT;(); 我希望这是你问的是什么

And results is private List<String> results = new ArrayList<String>(); I hope that is what you were asking.

推荐答案

您或许应该从的 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&LT;串GT;结果我想创建一个额外的对象的ArrayList&LT;整数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);
}

这篇关于如何从列表中,并从数据库中删除Android项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆