1

I’m Trying to Delete a row of listview from the list adapter where if a button is clicked it will look for the id and send it to the database for deletion. The method I wrote return the last id available and not the selected one. Here is the list Adapter

public class HistoryAdapter extends ArrayAdapter<NassrahHelper> {
int ID;
public HistoryAdapter(@NonNull Context context, ArrayList<NassrahHelper> resource) {
    super (context, R.layout.history_listadapter ,resource);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater ListInflate = LayoutInflater.from(getContext ());

    ValuesHelper data = getItem(position);

    View customView = ListInflate.inflate (R.layout.history_listadapter,parent,false);

    ID = data.H_ID;

    String first = data.Date;
    TextView date = (TextView) customView.findViewById (R.id.DateText);

    TextView DeleteBtn = (TextView) customView.findViewById(R.id.DeleteText);

    DeleteBtn.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    DeleteRecord();
                }
            }
    );

    date.setText (first);
    return customView;
}
private void DeleteRecord() {        
        DataBaseHelper dataBaseHelper = new DataBaseHelper(getContext());
                    dataBaseHelper.DeleteHistoryRow(ID);
                    dataBaseHelper.close();

}

This is the Database Class :

public boolean DeleteHistoryRow(int id) {
SQLiteDatabase database = this.getWritableDatabase ();

database.delete (HISTORY_TABLE, "ID = ?", new String[]{String.valueOf(id)});
Log.w ("DELETE History", "DELTE Secssuffle");
database.close ();
return true;

}

Ahmad Meer
  • 89
  • 1
  • 9

1 Answers1

1

In Delete button onClick method you have to get Id from the list using the poistion and need to pass that in delete record function.

DeleteBtn.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    ID = getItem(position).H_ID;
                    DeleteRecord();
                }
            }
    );

after delete the record from database and the list which you have assigned to adapter, you need to call notifiy data set changed method to reflect the listview.

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Perfect .But it gave me this "Variable 'position' is accessed from within inner class, needs to be declared final". so you will have to declare the position as final Thank you @ Chirag Raval – Ahmad Meer Jul 03 '17 at 10:14
  • how about refreshing the list after deletion if you know how i Tried notifyDataSetChanged(); – Ahmad Meer Jul 03 '17 at 10:19
  • You also have to delete the record from the list which you have assigned to the listview adapter. After that you have to call the notifyDataSetChanged method on adapter. – Chirag Jul 03 '17 at 10:27
  • It Gave an Error Saying "Non-static method 'notifyDataSetChanged()' cannot be referenced from a static context" – Ahmad Meer Jul 03 '17 at 10:29