如何从 firebase RecyclerView 中删除项目
How to remove items from firebase RecyclerView
我目前正在 firebase RecyclerView 的帮助下添加好友,如果用户点击添加按钮,他将被添加到数据库中 n 需要永久删除点击的项目..那么,如何通过 firebase 模型实现它,因为我们没有要从中删除的任何数据列表..
代码如下
@Override
public void onStart(){
super.onStart();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
EmailPref=prefs.getString("EmailPref", EmailPref);
if(EmailPref!=null){
final Firebase ref = new Firebase(constants.FIREBASE_URL+"rootssahaj/authGplus");
FirebaseRecyclerAdapter<FriendsData,FriendsViewHolder> adapter=new FirebaseRecyclerAdapter<FriendsData, FriendsViewHolder>(
FriendsData.class,
R.layout.chat_view_list,
FriendsViewHolder.class,
ref
) {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, FriendsData s, int position) {
String key = this.getRef(position).getKey();
Log.e("SahajLOG", "String Keyyy " + key + " n EmailPref" + EmailPref);
if (!Objects.equals(key, EmailPref)) {
friendsViewHolder.mName.setText(s.getUserNAME());
Picasso.with(context).load(s.getPicurl()).transform(new CircleTransform()).into(friendsViewHolder.mImageProfile);
String keylocal = key;
Log.e("pavan", "Populate AddF key" + keylocal+" " + position);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.getString("keylocal" + position, keylocal + position);
if (keylocal != null) {
prefs.edit().putString("keylocal" + position, keylocal + position).commit();
}
} else {
friendsViewHolder.mCardView.setVisibility(View.GONE);
//friendsViewHolder.setVisibility(View.GONE);
}
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new SlideInLeftAnimator());
recyclerView.getItemAnimator().setAddDuration(1000);
recyclerView.getItemAnimator().setRemoveDuration(1000);
recyclerView.getItemAnimator().setMoveDuration(1000);
recyclerView.getItemAnimator().setChangeDuration(1000);
SlideInLeftAnimator animator = new SlideInLeftAnimator();
animator.setInterpolator(new OvershootInterpolator());
recyclerView.setItemAnimator(animator);
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setDuration(500);
alphaAdapter.setInterpolator(new OvershootInterpolator(.5f));
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));
}
}
public class FriendsViewHolder extends RecyclerView.ViewHolder{
ImageView mImageProfile;
android.widget.TextView mName;
private int mVisibility;
Boolean CallAdd=false;
Button mAdd;
String keylocal;
CardView mCardView;
public FriendsViewHolder(View itemView) {
super(itemView);
mCardView=(CardView)itemView.findViewById(R.id.cardView);
mImageProfile=(ImageView)itemView.findViewById(R.id.profilePICC);
mName=(android.widget.TextView)itemView.findViewById(R.id.NameUSER);
context = itemView.getContext();
mAdd=(Button)itemView.findViewById(R.id.addButton);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if(prefs.getBoolean("CallAdd", CallAdd)){
AddaddButton();
}
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
EmailPref = prefs.getString("EmailPref", EmailPref);
keylocal=prefs.getString("keylocal"+getAdapterPosition(),keylocal+getAdapterPosition());
final Firebase mFirebaseRef = new Firebase(constants.FIREBASE_URL + "rootssahaj/authGplus/"+ EmailPref);
mFirebaseRef.child("friends").child(keylocal).setValue("true");
Log.e("pavan", "Chat Adapter " + EmailPref + keylocal + " final key: " + keylocal);
//remove(getAdapterPosition());
}
});
}
public void AddaddButton(){
if (mAdd.getVisibility() == View.INVISIBLE) {
mAdd.setVisibility(View.VISIBLE);
}
}
}
FirebaseUI FirebaseRecyclerAdapter
是底层 Query
或 Firebase
位置中数据的直接表示。要从适配器(和视图)中删除项目,您必须将其从 Firebase 位置删除(或确保它不再与查询匹配)。
所以当用户触发删除时(例如通过点击某个位置的项目):
adapter.getRef(position).remove()
如果您查看这段代码,它会在以下情况下从 Firebase 中删除一条注释
单击注释。如果您了解 Firebase 查询,这似乎很简单。一旦 RecyclerView 适配器设置正确,这也会更新 RecyclerView。您不需要数据列表,只需要要删除的值,例如 ID 或键。
@Override
public void onLongClick(View v, int i) {
mquery.orderByChild("text")
.equalTo((String) notes.get(i).getName())
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
firstChild.getRef().removeValue();
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
https://github.com/mmazzarolo/firebase-recyclerview 中的 FirebaseRecyclerAdapter.class
有效。
大多数情况下,我只是复制和粘贴。这是一个抽象 class,您可以使用它为您的 Firebase 数据库制作 RecyclerView 适配器。所有代码都在 GitHub 存储库中。这是 FirebaseRecyclerAdapter.class
中的一个片段,它从 Firebase 中删除了值并更新了 recyclerView:
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
if (mKeys.contains(key)) {
int index = mKeys.indexOf(key);
T item = mItems.get(index);
mKeys.remove(index);
mItems.remove(index);
notifyItemRemoved(index);
itemRemoved(item, key, index);
}
}
更新:您可以在 RecyclerView 适配器的 Viewholder 中隐藏一个视图。在 Whosebug Hiding views in RecyclerView
中查看这些答案
现在您可以轻松删除特定的 child
(1)- 第一步创建方法复制以下代码:
private void deleteRef(String id) {
databaseRef.child(id).removeValue();
}
(2) - 第二步在您想要的地方调用此方法,例如 AlertDialog
中的警报对话框或 firebase 回收器适配器复制如下:
public void alertDelete(final String id) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.warning_delete, null);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
final Button deete = dialogView.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRef(id);
alertDialog.dismiss();
Toast.makeText(Teachers_Admin_Activity.this,"Delete Successfully",Toast.LENGTH_LONG).show();
}
});
}
(3) 或者像这样从recyclerview中删除child节点,但是记住一定要从ViewHolder
:
调用
@Override
protected void onBindViewHolder(@NonNull final BlogViewHolder holder, final int position, @NonNull final TeachersName model) {
// this is method in viewholer where you next step to delete specific child node //
DatabaseReference itemRef = getRef(position);
final String myKeyItem = itemRef.getKey();
}
(4)- 点击监听器的最后一步:
holder.deleteImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
alertDelete(myKeyItem);
return true;
}
});
我目前正在 firebase RecyclerView 的帮助下添加好友,如果用户点击添加按钮,他将被添加到数据库中 n 需要永久删除点击的项目..那么,如何通过 firebase 模型实现它,因为我们没有要从中删除的任何数据列表..
代码如下
@Override
public void onStart(){
super.onStart();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
EmailPref=prefs.getString("EmailPref", EmailPref);
if(EmailPref!=null){
final Firebase ref = new Firebase(constants.FIREBASE_URL+"rootssahaj/authGplus");
FirebaseRecyclerAdapter<FriendsData,FriendsViewHolder> adapter=new FirebaseRecyclerAdapter<FriendsData, FriendsViewHolder>(
FriendsData.class,
R.layout.chat_view_list,
FriendsViewHolder.class,
ref
) {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, FriendsData s, int position) {
String key = this.getRef(position).getKey();
Log.e("SahajLOG", "String Keyyy " + key + " n EmailPref" + EmailPref);
if (!Objects.equals(key, EmailPref)) {
friendsViewHolder.mName.setText(s.getUserNAME());
Picasso.with(context).load(s.getPicurl()).transform(new CircleTransform()).into(friendsViewHolder.mImageProfile);
String keylocal = key;
Log.e("pavan", "Populate AddF key" + keylocal+" " + position);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.getString("keylocal" + position, keylocal + position);
if (keylocal != null) {
prefs.edit().putString("keylocal" + position, keylocal + position).commit();
}
} else {
friendsViewHolder.mCardView.setVisibility(View.GONE);
//friendsViewHolder.setVisibility(View.GONE);
}
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new SlideInLeftAnimator());
recyclerView.getItemAnimator().setAddDuration(1000);
recyclerView.getItemAnimator().setRemoveDuration(1000);
recyclerView.getItemAnimator().setMoveDuration(1000);
recyclerView.getItemAnimator().setChangeDuration(1000);
SlideInLeftAnimator animator = new SlideInLeftAnimator();
animator.setInterpolator(new OvershootInterpolator());
recyclerView.setItemAnimator(animator);
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setDuration(500);
alphaAdapter.setInterpolator(new OvershootInterpolator(.5f));
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));
}
}
public class FriendsViewHolder extends RecyclerView.ViewHolder{
ImageView mImageProfile;
android.widget.TextView mName;
private int mVisibility;
Boolean CallAdd=false;
Button mAdd;
String keylocal;
CardView mCardView;
public FriendsViewHolder(View itemView) {
super(itemView);
mCardView=(CardView)itemView.findViewById(R.id.cardView);
mImageProfile=(ImageView)itemView.findViewById(R.id.profilePICC);
mName=(android.widget.TextView)itemView.findViewById(R.id.NameUSER);
context = itemView.getContext();
mAdd=(Button)itemView.findViewById(R.id.addButton);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if(prefs.getBoolean("CallAdd", CallAdd)){
AddaddButton();
}
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
EmailPref = prefs.getString("EmailPref", EmailPref);
keylocal=prefs.getString("keylocal"+getAdapterPosition(),keylocal+getAdapterPosition());
final Firebase mFirebaseRef = new Firebase(constants.FIREBASE_URL + "rootssahaj/authGplus/"+ EmailPref);
mFirebaseRef.child("friends").child(keylocal).setValue("true");
Log.e("pavan", "Chat Adapter " + EmailPref + keylocal + " final key: " + keylocal);
//remove(getAdapterPosition());
}
});
}
public void AddaddButton(){
if (mAdd.getVisibility() == View.INVISIBLE) {
mAdd.setVisibility(View.VISIBLE);
}
}
}
FirebaseUI FirebaseRecyclerAdapter
是底层 Query
或 Firebase
位置中数据的直接表示。要从适配器(和视图)中删除项目,您必须将其从 Firebase 位置删除(或确保它不再与查询匹配)。
所以当用户触发删除时(例如通过点击某个位置的项目):
adapter.getRef(position).remove()
如果您查看这段代码,它会在以下情况下从 Firebase 中删除一条注释 单击注释。如果您了解 Firebase 查询,这似乎很简单。一旦 RecyclerView 适配器设置正确,这也会更新 RecyclerView。您不需要数据列表,只需要要删除的值,例如 ID 或键。
@Override
public void onLongClick(View v, int i) {
mquery.orderByChild("text")
.equalTo((String) notes.get(i).getName())
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
firstChild.getRef().removeValue();
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
https://github.com/mmazzarolo/firebase-recyclerview 中的 FirebaseRecyclerAdapter.class
有效。
大多数情况下,我只是复制和粘贴。这是一个抽象 class,您可以使用它为您的 Firebase 数据库制作 RecyclerView 适配器。所有代码都在 GitHub 存储库中。这是 FirebaseRecyclerAdapter.class
中的一个片段,它从 Firebase 中删除了值并更新了 recyclerView:
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
if (mKeys.contains(key)) {
int index = mKeys.indexOf(key);
T item = mItems.get(index);
mKeys.remove(index);
mItems.remove(index);
notifyItemRemoved(index);
itemRemoved(item, key, index);
}
}
更新:您可以在 RecyclerView 适配器的 Viewholder 中隐藏一个视图。在 Whosebug Hiding views in RecyclerView
中查看这些答案现在您可以轻松删除特定的 child
(1)- 第一步创建方法复制以下代码:
private void deleteRef(String id) {
databaseRef.child(id).removeValue();
}
(2) - 第二步在您想要的地方调用此方法,例如 AlertDialog
中的警报对话框或 firebase 回收器适配器复制如下:
public void alertDelete(final String id) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.warning_delete, null);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
final Button deete = dialogView.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRef(id);
alertDialog.dismiss();
Toast.makeText(Teachers_Admin_Activity.this,"Delete Successfully",Toast.LENGTH_LONG).show();
}
});
}
(3) 或者像这样从recyclerview中删除child节点,但是记住一定要从ViewHolder
:
@Override
protected void onBindViewHolder(@NonNull final BlogViewHolder holder, final int position, @NonNull final TeachersName model) {
// this is method in viewholer where you next step to delete specific child node //
DatabaseReference itemRef = getRef(position);
final String myKeyItem = itemRef.getKey();
}
(4)- 点击监听器的最后一步:
holder.deleteImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
alertDelete(myKeyItem);
return true;
}
});