1

大家好我如何删除自定义列表视图中的单击项目。我无法删除Firebase数据。Android使用自定义列表视图删除Firebase数据

这里是学生活动,所有学生将出现在列表视图定制我要上点击删除

@覆盖 保护无效在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) { 

     } 
    }); 
} 

}

这里是我的自定义适配器类数组适配器扩展

@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; 

这里是ModelClass .. ModelClass包含学生和公司领域的火力点,以节省..下面检查图像

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

+0

你能告诉我们你的ModelClass吗? – Pipiks

+0

嘿Pipiks,我添加我的ModelClass的问题..我正确能够通过ModelClass保存我的数据,如果用户在Firebase数据保存类型“公司”,如果用户说“公司”,否则“学生”..但我无法删除在哪里我的所有学生都出现在自定义列表中 –

+0

如果你这么说,我会在GitHub上更新我的代码,这样你可以检查我的所有代码..如果你帮我,我会很高兴..我浪费了4个小时,但现在我自己失败 –

回答

0

您需要保存在模型中的用户密钥,所以补充一点:

public String 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()。

+0

嘿皮皮克斯,我成功了删除我的数据onclick ..谢谢你的代码帮助我:) –

+0

你能告诉我'databseReference'的初始化 – Pipiks

+0

databaseReference = FirebaseDatabase.getInstance()。getReference(“users”); –