0

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

As shown below:

enter image description here

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;
}
4
  • 1
    And the problem is? Commented Apr 4, 2017 at 7:42
  • it deletes everything because the method that you are calling deletes everything!! What exactly is your question? Commented Apr 4, 2017 at 7:46
  • What problem are you facing ? Commented Apr 4, 2017 at 7:48
  • My problem is that when I re-stored information. This information is stored on the previous item. Commented Apr 4, 2017 at 7:50

1 Answer 1

2

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();

                                    }
Sign up to request clarification or add additional context in comments.

1 Comment

@shekari Happy to hear .Move ahead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.