简体   繁体   中英

Delete item from database - ListView in Android Studio

I have a listview in my program and I have three buttons.

As shown below:

在此处输入图片说明

I want when I click on the delete button to remove any item from the list view and the database.

I tried to delete but was cleared of all items.

Please Help me.thanks

My database helper :

public class DatabaseHelper1 extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist_data";
public static final String COL_1 = "ID";
public static final String COL_2 = "billID";
public static final String COL_3 = "payID";
public static final String COL_4 = "txtLOCATION";
public static final String COL_5 = "txtTYPE";


public DatabaseHelper1(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            " billID INTEGER, payID INTEGER, txtLOCATION TEXT, txtTYPE TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public boolean addData(String billid, String payid, String txtlocation,
                       String txttype) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, billid);
    contentValues.put(COL_3, payid);
    contentValues.put(COL_4, txtlocation);
    contentValues.put(COL_5, txttype);

    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
}
    public void deleteAll()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(DatabaseHelper1.TABLE_NAME, null, null);
    }
}

My list adapter :

public class FourColumn_ListAdapter extends ArrayAdapter<User> {

DatabaseHelper1 myDB;
private LayoutInflater mInflater;
private ArrayList<User> users;
private int mViewResourceId;


public FourColumn_ListAdapter(Context context, int textViewResourcesId, ArrayList<User> users){
    super(context,textViewResourcesId, users);
    this.users = users;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = textViewResourcesId;
}

@NonNull
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

    convertView = mInflater.inflate(mViewResourceId, null);

    final User user = users.get(position);

    if (user != null){
        TextView billId = (TextView) convertView.findViewById(R.id.bilid);
        TextView payId = (TextView) convertView.findViewById(R.id.pyid);
        TextView location = (TextView) convertView.findViewById(R.id.lc);
        TextView types = (TextView) convertView.findViewById(R.id.noe);

        if(billId != null){
            billId.setText((user.getBillID()));
        }
        if(payId != null){
            payId.setText((user.getPayID()));
        }
        if(location != null){
            location.setText((user.getLocation()));
        }
        if(types != null){
            types.setText((user.getTypes()));
        }
        final Button button = (Button)convertView.findViewById(R.id.btnDelete);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(getContext())
                        .setTitle("حذف قبض")
                        .setMessage("آیا از حذف این قبض اطمینان دارید؟ ")
                        .setPositiveButton("بله",
                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int id) {
                                        myDB = new DatabaseHelper1(getContext());
                                        myDB.deleteAll();
                                        users.remove(position);
                                        notifyDataSetChanged();


                                    }
                                })
                        .setNegativeButton("بیخیال", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

                                dialog.cancel();
                            }
                        }).show();


        }
        });

    }
    return convertView;
}

When I click on the delete button to remove any item from the list view and the database

Create a DELETE Method

  public void deleteItem(String getID) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE from mylist_data  WHERE billID= '" +getID+ "'");
    }

Then

    public void onClick(DialogInterface dialog, int id) 
                                   {
                                    myDB = new DatabaseHelper1(getContext());
                                    myDB.deleteItem(user.getBillID());
                                    users.remove(position);
                                    notifyDataSetChanged();

                                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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