Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

android - How to remove items from firebase RecyclerView

I'm currently working on Adding Friends with the help of firebase RecyclerView in which if a user tap on ADD button, he is added in database n that tapped item is needed to be removed permanently.. so, how to achieve it through firebase model as we dont have any data list to remove from..

Here's the code

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...