通过ListView和id删除数据库项

时间:2015-12-09 12:54:24

标签: android sqlite android-listview

我有一个带有ListView的Android应用和我本地sql数据库的数据。 现在我想用ID删除我数据库中的项目。

我想对任何一行进行长按,这应该删除该项目。

 ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// DELETE ACTION
}

但是如何获取数据库的项目ID? 目前,我通过查询将ID保存在隐藏的TextView中,并通过findviewbyid()获取此ID。

但这是正确的方法吗?

适配器:

public class DatabaseListAdapter extends BaseAdapter {

Context context;
ArrayList<DatabaseListItems> databaseList;


public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
    this.context = context;
    databaseList = list;
}

@Override
public int getCount() {
    return databaseList.size();
}

@Override
public Object getItem(int position) {
    return databaseList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    DatabaseListItems databaseListItems = databaseList.get(position);


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_listview_item, null);

    }

    TextView tvID = (TextView) convertView.findViewById(R.id.txtViewID);
    tvID.setText(databaseListItems.getID());

    return convertView;
}

}

5 个答案:

答案 0 :(得分:0)

在listview中创建一个id和title以及sotre的模型类。对于前。

public class Data{
 private int id;
 private String title;

 public Data(int id,String title){
     this.id = id;
     this.title = title;

 }
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
}

您填写列表视图的位置执行此操作

list.add(new Data(1,"xyz"))

所以内部点击事件

ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
   int id  = list.get(posttion).getId(); //you can get id like this
}

答案 1 :(得分:0)

我建议您CursorAdapter使用ListView。如果是,我建议您从Cursor中的bindView()对象获取ID。使用ViewHolder模式将一些信息附加到ListView行。

class ViewHolder{
TextView text;
ImageView image;
...
long id;}

为其添加长ID并在长按一下检索。

答案 2 :(得分:0)

让你的适配器实现onItemLongClickListener。

public class DatabaseListAdapter extends BaseAdapter implements AdapterView.OnItemLongClickListener {

...

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

    long itemID = databaseList.get(position).getID();

    //Perform delete

    return true;
}

因此,您可以像在getView()方法中那样访问项目的ID。

答案 3 :(得分:0)

基本上你的适配器应该有数据,数据应该是要删除的Item的id,所以我们假设每个适配器都有一个名为dataArrayList的arraylist包含你的适配器数据,当我们单击该按钮,我们删除db中的项目,所以它看起来像:

 deletebtn.setOnItemLongClickListener(->(position){

   Contact mContact = dataArrayList.get(position).getContact();
   deleteContact(mContact);

 //adapter this then we can remove the listview adapter row whose data
 //we just deleted in the db
    arrayAdapter.getItem(position);
    arrayAdapter.remove(toRemove);
    arrayAdapter.notifyDatasetChange();
  //finally we want to remove the arraylist data too from it

  dataArrayList.remove(position);
}


//Our Delete method in our database class

public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
        new String[] { String.valueOf(contact.getID()) });
db.close();
}

答案 4 :(得分:0)

创建POJO类项目或id

class info{
    private String item;
    private int id;
    info(String item,int id){
        this.item = item;
        this.id = id;
    }
    public setId(int id){
        this.id=id;
    }
    public setItem(String item){
        this.item=item;
    }
    public int getId(){
        return id;
    }
    public String getItem(){
        return item;
    } 
}

并将对象存储到ArrayList中,以便获取具有位置的特定项目的ID

ArrayList.get(position).getId();
ArrayList.remove(position);
notifyDatasetChange();

简单地创建两个像ArrayList一样的

ArrayList<String> mItem = new ArrayList<String>();
ArrayList<String> mId = new ArrayList<String>();

然后你可以获得特定位置的id并从arraylist中删除

mId.get(position);
mId.remove(position);
mItem.remove(position);
notifyDatasetChange();