我使用了带有cardivew的Recycleview以及数据列表和按钮来删除任何值。当我单击按钮时,它的确从Firebase数据库中删除了该值,但文本和行仍保留在应用程序的适配器中,但仅当我通过返回并重新打开关闭活动,然后行为空时。
我每次在两个地方使用下面的方法(我也在代码中显示过),但是它不起作用
bookslist.remove(position);
notifyDataSetChanged();
当我点击删除按钮时,如何立即更新回收视图
这是我的代码
public class SubjectBooksAdapter extends RecyclerView.Adapter<SubjectBooksAdapter.MyViewHolder> {
ArrayList<Books> bookslist;
FirebaseDatabase database;
DatabaseReference dbreference;
public SubjectBooksAdapter(ArrayList<Books> bookslist){
this.bookslist = bookslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Button mSolved;
MyViewHolder(final View itemView) {
super(itemView);
database = FirebaseDatabase.getInstance();
dbreference = database.getReference("roomrequest");
dbreference.keepSynced(true);
mSolved = (Button) itemView.findViewById(R.id.book_solved);
mSolved.setTransformationMethod(null);
fauth = FirebaseAuth.getInstance();
}
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
database = FirebaseDatabase.getInstance();
dbreference = database.getReference("roomrequest");
holder.mSolved.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classicalMechanicsRef = rootRef.child("roomrequest").child("request").child(g.getRequestid());
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
bookslist.remove(position); \\ Solution One
notifyDataSetChanged(); \\ Solution One
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
classicalMechanicsRef.addListenerForSingleValueEvent(valueEventListener);
bookslist.remove(position); \\ Solution Two
notifyDataSetChanged(); \\ Solution Two
}
});
}
@Override
public int getItemCount() {
return bookslist.size();
}
}
答案 0 :(得分:0)
使用 Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.42,
child: Padding(
padding: EdgeInsets.only(left: 0.0),
child: Text("الجنس"))),
Flexible(
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
value: _data.gender,
isDense: true,
onChanged: (String newValue) {
setState(() {
_data.gender = newValue;
});
},
items: genders.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
),
),
),
],
),
例如-
notifyItemRemoved(position);