'remove and update items in listview at firebase database
May you help us with our school project. We are trying to delete and update items in listview and firebase database. The delete works but only delete the values from the listview without deleting it from firebase database i have been working to solve this problem long time ago i tried several solutions but nothing works please help me. As for the update It's just add new item with the updated info but never delete the updated item.please check the images to get a reference for the update problem.
push() --> in Additem.class
private void StoreProductInformation() {
Calendar calForDate=Calendar.getInstance();
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calForDate.getTime());
Description = InputProductDescription.getText().toString();
Pname = InputProductName.getText().toString();
Pkey= ProductsRef.push().getKey().toString();
citem obj = new citem(Pname,Description, Pkey);
ProductsRef.push().setValue(obj);
Toast.makeText(Additem.this, "Added To cart", Toast.LENGTH_SHORT).show();
Intent i =new Intent(this,shoppingcart.class);
startActivity(i);
}
Citem.class
public class citem {
String name;
String qty;
String key;
public citem() { }
public citem(String name, String qty, String key ) {
this.name = name;
this.qty = qty;
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
remove & update --> shoppingcart.class
public class shoppingcart extends AppCompatActivity {
ListView list;
FirebaseDatabase d;
DatabaseReference myRef;
ArrayList<String> mylist;
ArrayAdapter<String> myadapter;
citem i ;
Button r ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoppingcart);
r=findViewById(R.id.Button02);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(shoppingcart.this, order.class);
startActivity(i);
finish();
}
});
list =(ListView) findViewById(R.id.ListViewCatalog);
d = FirebaseDatabase.getInstance();
myRef = d.getReference("shoppingcart");
mylist= new ArrayList<>();
myadapter = new ArrayAdapter<String>(this,R.layout.activity_test,R.id.dtext,mylist);
i = new citem();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
i = ds.getValue(citem.class);
mylist.add(" "+ i.getName()+ "\n" + " "+ i.getQty());
// mylist.add(new citem(i.name,i.qty,i.key));
}
list.setAdapter(myadapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
list.setAdapter(myadapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(shoppingcart.this);
LinearLayout layout = new LinearLayout(shoppingcart.this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText name_et = new EditText(shoppingcart.this);
layout.addView(name_et);
name_et.setHint("Qty");
builder.setTitle("Edit Item")
.setMessage("Write new qty to Update")
.setView(layout)
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseReference uref = FirebaseDatabase.getInstance().getReference("shoppingcart").child(i.getKey());
citem m = new citem(i.getName(), name_et.getText().toString().trim(),i.getKey());
uref.setValue(m);
}
}).setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseReference dref = FirebaseDatabase.getInstance().getReference("shoppingcart").child(i.getKey());
mylist.remove(position);
myadapter.notifyDataSetChanged();
}
}).show();
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|