How to reference to the parent from child in firebase database while showing in recyclerView usin Firebase UI

馋奶兔 提交于 2019-12-11 00:45:44

问题


I am currently referencing to the quote_text child. These data are showing in recyclerview using firebase UI. I want to get parent name of quote_text. How do you get parent name of quote_text child.

When user clicks favorite button then I want to get the quote_id and want to create another key. Please help me.

Here is code of my project:

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Quotes, QuoteHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull QuoteHolder holder, int position, @NonNull Quotes model) {
            Log.i("KK", "onBindViewHolder" + model.getQuote_text());
            Log.i("KK", "onBindViewHolder AUTHER" + model.getAuther());
            //Setting Quotes
            holder.setQuote(model.getQuote_text());

            //Setting Likes Count
            holder.setLikesCount(model.getLikes_count());
        }

        @Override
        public QuoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.quote_single_layout, parent, false);

            Log.i("KK", "Inflate" + view);

            return new QuoteHolder(view);
        }
    };
    Log.i("KK", "Adapter" + firebaseRecyclerAdapter);
    mRecylerViewQuote.setAdapter(firebaseRecyclerAdapter);

}

@Override
protected void onStart() {
    super.onStart();

    firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStop() {
    super.onStop();
    firebaseRecyclerAdapter.stopListening();
}

public class QuoteHolder extends RecyclerView.ViewHolder{

    View view;

    //Two share and favorite image buttons
    private ImageButton mShareButton;
    private ImageButton mFavoriteButton;
    //String
    String quoteText;


    public QuoteHolder(View itemView) {
        super(itemView);
        view = itemView;

        //initialization
        mShareButton = view.findViewById(R.id.share_button);
        mFavoriteButton = view.findViewById(R.id.favorite_button);

        //Click event Handling
        mShareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(view.getContext(), "Share Button Clicked", Toast.LENGTH_SHORT).show();
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, quoteText);
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, "Share Using:"));
            }
        });

        mFavoriteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(view.getContext(), "Favorite Button Clicked", Toast.LENGTH_SHORT).show();

                setFavoriteQuote()//Here I want to get parent name of 
                                  //quote_text......................
            }
        });

    }

    public void setQuote(String quote_text) {
        quoteText = quote_text;

        TextView mQuoteText = view.findViewById(R.id.quote_text);
        mQuoteText.setText("\"" + quote_text + "\"");

        if (mQuoteProgress.isShowing())
            mQuoteProgress.dismiss();
    }

    public void setLikesCount(int likes_count) {
        TextView mLikesCountText = view.findViewById(R.id.likes_count_text);
        Log.i("Count", String.valueOf(likes_count));
        mLikesCountText.setText(String.valueOf(likes_count) + "  Likes");

        if (mQuoteProgress.isShowing())
            mQuoteProgress.dismiss();
    }
}

回答1:


You can access the data snapshots by using the getSnapshots() method inside your adapter. Like this:

@Override
protected void onBindViewHolder(@NonNull QuoteViewHoler holder, int position, @NonNull Quote model) {
    this.getSnapshots().getSnapshot(position).getKey();
}


来源:https://stackoverflow.com/questions/48640230/how-to-reference-to-the-parent-from-child-in-firebase-database-while-showing-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!