Reputation:
I have an app and when the user clicked on an item from listview the information about this item will show up in another page, the user can click on edit or delete button to remove this item from the list.
The problem is that the code of the delete is not working. The detail page just closes and returns to list page without delete or modify anything.
list page
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in=new Intent(showAll.this,editCar.class);
in.putExtra("car_id",position);
startActivity(in);
}
});
detail page
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_car);
Button btnDelete=(Button) findViewById(R.id.button4);
Button btnEdit=(Button)findViewById(R.id.edut);
final EditText comName=(EditText)findViewById(R.id.edit_comp);
final EditText model=(EditText)findViewById(R.id.edit_model);
final CheckBox status=(CheckBox)findViewById(R.id.checkbox);
final int i=getIntent().getIntExtra("car_id",0);
Realm.init(getApplicationContext());
final Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
final RealmResults<Car> cars =realm.where(Car.class).findAll();
realm.commitTransaction();
final Car car3=cars.get(i);
realm.close();
comName.setText(car3.getName());
model.setText(car3.getModel());
status.setChecked(car3.isStatus());
final Context context=this;
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(editCar.this).setTitle("warning").setMessage("are you sure you want to delete this car ?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Realm.init(getApplicationContext());
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
car3.deleteFromRealm();
realm.commitTransaction();
realm.close();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() { //if user choose no do nothing
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"no",Toast.LENGTH_SHORT).show();
}
}).show(); //show dialog
Upvotes: 2
Views: 118
Reputation: 1925
Add this functionality on click of your delete button. This is working for me. :)
Here Notifications
is my model class.
Realm realm = Realm.getDefaultInstance();
final Notifications deletedItem = yourArrayList.get(indexPosition);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
RealmResults<Notifications> **rows** = realm.where(Notifications.class).equalTo("columnKeyName", columnKeyValue).findAll();
**rows**.deleteAllFromRealm();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
final int deletedIndex = indexPosition;
// remove the item from recycler view and notify adapter
yourArrayList.remove(deletedIndex);
adapter.notifyDataSetChanged();
Hope this will also helpful to you. :)
Upvotes: 1