Android 使用自定义列表视图删除 firebase 数据
Android delete firebase data using custom listview
大家好我如何从自定义列表视图中删除点击项目。我无法删除 firebase 数据。
这是学生 Activity 所有学生都将出现在自定义列表视图中,我想在点击时删除
@Override
受保护的 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) {
}
});
}
}
这是我的自定义适配器 class 扩展阵列适配器
@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;
这是模型类。模型类包含要保存在 firebase 中的学生和公司字段。检查下图
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;
}
}
Here is the Database Image Click on this
您需要在模型中保存用户密钥,因此添加:
public String key;
然后就可以设置key了:
ModelClass Mod = dataSnapshot1.getValue(ModelClass.class);
Mod.key = dataSnapshot1.getKey();
list.add(Mod);
并且在您的 onItemClick 中,您现在可以删除当前用户:
@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();
}
注意
您应该使用 childAdded 事件来避免在删除用户时重新加载所有列表。
你也应该只初始化你的适配器一次。在完成后更新列表并调用 notifyDataChanged()。
大家好我如何从自定义列表视图中删除点击项目。我无法删除 firebase 数据。
这是学生 Activity 所有学生都将出现在自定义列表视图中,我想在点击时删除
@Override 受保护的 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) {
}
});
}
}
这是我的自定义适配器 class 扩展阵列适配器
@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;
这是模型类。模型类包含要保存在 firebase 中的学生和公司字段。检查下图
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;
}
}
Here is the Database Image Click on this
您需要在模型中保存用户密钥,因此添加:
public String key;
然后就可以设置key了:
ModelClass Mod = dataSnapshot1.getValue(ModelClass.class);
Mod.key = dataSnapshot1.getKey();
list.add(Mod);
并且在您的 onItemClick 中,您现在可以删除当前用户:
@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();
}
注意
您应该使用 childAdded 事件来避免在删除用户时重新加载所有列表。
你也应该只初始化你的适配器一次。在完成后更新列表并调用 notifyDataChanged()。