简体   繁体   English

Android-从ListView中删除项目,但不从数据库中删除项目

[英]Android - deleting items from ListView but not from Database

So I have a small problem, my code deletes ListView row from ListView but every time I kill the app and then reopen it the "deleted" rows populate the ListView again. 所以我有一个小问题,我的代码从ListView中删除了ListView行,但是每次我杀死该应用程序,然后重新打开它时,“已删除”的行会再次填充ListView。

Here's the code for delete method in DatabaseHelper class: 这是DatabaseHelper类中delete方法的代码:

 public void obrisiTrening(int id){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(DBKonstante.TABLE_NAME, DBKonstante.KEY_ID + "=?", new String[]{String.valueOf(id)});
        db.close();

And here's what my code for deleting ListView row and record from database: 这是我从数据库中删除ListView行和记录的代码:

  rec_WorkoutItemsList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {


            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.dialog_delete);

            final TextView tvDialogDelete = (TextView) dialog.findViewById(R.id.tvDialogDelete);

            tvDialogDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final DBPodatci infoData = dbPodatci.get(i);
                    dba = new DBHandler(MainActivity.this);
                    int position = dbPodatci.indexOf(infoData);
                    dbPodatci.remove(position);

                    DBPodatci podatki = new DBPodatci();
                    final int idToDelete = podatki.getItemId();

                    dba.obrisiTrening(idToDelete);
                    dba = new DBHandler(MainActivity.this);
                    dba.obrisiTrening(i);

                    rec_WorkoutItemsList.setAdapter(vjezbaAdapter);
                    vjezbaAdapter.notifyDataSetChanged();
                    dialog.dismiss();

                }
            });
            dialog.show();

            return false;
        }
    });

DB PODATCI DB PODATCI

public class DBPodatci {
    public String odabraneVjezbe, recordDate;
    public int itemId;

    public String getOdabraneVjezbe() {
        return odabraneVjezbe;
    }

    public void setOdabraneVjezbe(String odabraneVjezbe) {
        this.odabraneVjezbe = odabraneVjezbe;
    }

    public String getRecordDate() {
        return recordDate;
    }

    public void setRecordDate(String recordDate) {

    this.recordDate = recordDate;
}

public int getItemId() {
    return itemId;
}

public void setItemId(int itemId) {
    this.itemId = itemId;
}

} }

Not sure what this is trying to do. 不知道这是要做什么。

dba.obrisiTrening(idToDelete);
dba = new DBHandler(MainActivity.this);
dba.obrisiTrening(i);

You only need this 你只需要这个

   final TextView tvDialogDelete = (TextView) dialog.findViewById(R.id.tvDialogDelete);

    final DBHandler dba = new DBHandler(MainActivity.this);

        tvDialogDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final DBPodatci infoData = dbPodatci.get(i);
                final int idToDelete = infoData.getItemId();

                dbPodatci.remove(i);  
                dba.obrisiTrening(idToDelete);

                vjezbaAdapter.notifyDataSetChanged();
                dialog.dismiss();

And note: you shouldnt be using an Arraylist & ArrayAdapter here... You are using a database, so CursorAdapter is what you want 并注意:您不应该在这里使用Arraylist和ArrayAdapter ...您正在使用数据库,因此CursorAdapter是您想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM