关于java:如何从-firebase-RecyclerView-中删除项目

44次阅读

共计 4580 个字符,预计需要花费 12 分钟才能阅读完成。

我目前正在 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);
    }
}

}
解决方案

如果您查看此代码,它会在以下状况下从 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://www.itbaoku.cn/ . 大多数状况下,我只是复制和粘贴. 这是一个抽象类,可用于为 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 中查看这些答案 在 RecyclerView 中暗藏视图

原文地址:https://www.itbaoku.cn/post/2…

正文完
 0