Android在RecyclerView OnClickListener中获取firebase推送ID

时间:2016-06-23 05:31:33

标签: android firebase firebase-realtime-database

我正在使用通用 Recyclerview 来填充来自firebase的数据,我想知道如何从 firebase 获取推送ID,以便我可以进入详情屏幕?

我尝试使用

DatabaseReference mDatabaseReference = getRef(position); 

在onBindViewHolder中但它无法正常工作

我知道我可以从 FirebaseRecyclerAdapter 获取推送ID,但在这种情况下,我使用的是通用 Recyclerview

private class SimpleAdapter extends RecyclerView.Adapter<SimpleViewHolder> {

    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item_recyclerview, parent, false);
        return new SimpleViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
        DatabaseReference mDatabaseReference = getRef(position);
        final String post_key = mDatabaseReference.getKey();

        holder.tvShopTitle.setText(myList.get(position).getTitle());
        holder.tvShopAddress.setText(myList.get(position).getAddress());

        holder.mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent mIntent = new Intent(getActivity(), DetailActivity.class);
                mIntent.putExtra("Key", post_key);
                mIntent.putExtra("Title", myList.get(position).getTitle());
                getActivity().startActivity(mIntent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return myList.size();
    }
}

感谢您的时间:)

1 个答案:

答案 0 :(得分:1)

以下是适合我的解决方案:

这是一种使用它向firebase数据库插入新值的方法:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference myRef = firebaseDatabase.getReference().child("Post").push();

    Map<String, Object> map = new HashMap<>();
    map.put("Title", "dummy title");
    map.put("Address", "dummy address");
    map.put("PostID", myRef.getKey());
    myRef.setValue(map);

现在我已将push id存储在firebase数据库中,然后我可以检索该值以及其他数据并按如下方式填充recyclerview:

@Override
public void onBindViewHolder(final SimpleViewHolder holder, final int position) {

    final String post_key = myList.get(position).getShopID();

    holder.tvShopTitle.setText(myList.get(position).getTitle());
    holder.tvShopAddress.setText(myList.get(position).getAddress());

    holder.mLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent mIntent = new Intent(getActivity(), DetailActivity.class);
            mIntent.putExtra("Key", post_key);
            mIntent.putExtra("Title", myList.get(position).getTitle());
            getActivity().startActivity(mIntent);
        }
    });

}

以下是我的Pojo课程的样子:

public class MyPojo {

    private String Title;
    private String Address;
    private String ShopID;

    public MyPojo() {
    }

    public MyPojo(String title, String address, String shopID) {
        Title = title;
        Address = address;
        ShopID = shopID;
    }
    public String getTitle() {
        return Title;
    }

    public String getAddress() {
        return Address;
    }

    public String getShopID() {
        return ShopID;
    }
}