我目前正在使用firebase RecyclerView帮助添加好友,其中如果用户点击ADD按钮,则会在数据库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);
}
}
}
答案 0 :(得分:12)
FirebaseUI FirebaseRecyclerAdapter
直接代表基础Query
或Firebase
位置的数据。要从适配器(和视图)中删除项目,您必须将其从Firebase位置删除(或确保它不再与查询匹配)。
因此,当用户触发删除时(例如,通过点击特定位置的项目):
adapter.getRef(position).remove()
答案 1 :(得分:5)
如果您查看此代码,则会从Firebase中删除Note 单击注释。如果您了解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
有效。
在大多数情况下,我只是复制和粘贴。这是一个抽象类,可用于为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中隐藏视图。在stackoverflow Hiding views in RecyclerView
中查看这些答案答案 2 :(得分:0)
现在您可以轻松删除特定的孩子
(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子节点删除,但请记住必须从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)-onclicklistener的最后一步:
holder.deleteImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
alertDelete(myKeyItem);
return true;
}
});