Hello Everyone how i can delete on click item from custom listview. i am unable to delete the firebase data.
Here is the Student Activity where all student will appear in custom listview i want to delete on click
@Override protected void onStart() { super.onStart();
Query query = databaseReference.orderByChild("type").equalTo("student");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
ModelClass Mod = dataSnapshot1.getValue(ModelClass.class);
list.add(Mod);
}
final CustomAdapterClassStudent adapterClassCompany = new CustomAdapterClassStudent(AllStudents.this , list);
l1.setAdapter(adapterClassCompany);
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Here is my Custom Adapter class extend with Array Adapter
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listview = inflater.inflate(R.layout.customadapter , null , true);
TextView textViewName = (TextView) listview.findViewById(R.id.textName);
TextView textViewAdd = (TextView) listview.findViewById(R.id.textAdd);
TextView textViewBio = (TextView) listview.findViewById(R.id.textBio);
final ModelClass Mod = compniesModel.get(position);
textViewName.setText(Mod.name);
textViewAdd.setText(Mod.cgpa);
textViewBio.setText(Mod.bio);
return listview;
Here is the ModelClass.. ModelClass contain student and company fields to save in firebase.. check image below
public class ModelClass {
public String name;
public String email;
public String cgpa;
public String age;
public String bio;
public String type;
public String compName;
public String compAdd;
public String compAbout;
public ModelClass(){}
public ModelClass(String name, String email, String cgpa, String age, String bio,String type) {
this.name = name;
this.email = email;
this.cgpa = cgpa;
this.age = age;
this.bio = bio;
this.type = type;
}
public ModelClass(String compName, String compAdd, String compAbout , String type) {
this.compName = compName;
this.compAdd = compAdd;
this.compAbout = compAbout;
this.type = type;
}
}
You need to save the user key in your model, so add this :
public String key;
Then you can set the key :
ModelClass Mod = dataSnapshot1.getValue(ModelClass.class);
Mod.key = dataSnapshot1.getKey();
list.add(Mod);
And in your onItemClick you can now remove the current user :
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ModelClass model = list.get(i);
list.remove(removePosition);
databaseReference.child("users").child(model.key).removeValue();
}
NOTE
You should use childAdded event to not reload all your list when you remove a user.
You should also init your adapter only one time. In your completion update the list and call notifyDataChanged().