android FirebaseRecyclerAdapter单击项目不起作用

时间:2018-09-03 10:22:42

标签: android firebase firebase-realtime-database firebaseui

我在我的android项目中显示了Firebase数据库中的一些数据,但是当我单击该项目时,什么都没有触发,这意味着单击不起作用

这是我的活动

public class Notification extends AppCompatActivity {
    private RecyclerView mPeopleRV;
    private DatabaseReference mDatabase;
    private FirebaseRecyclerAdapter<News, Notification.NewsViewHolder> mPeopleRVAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("XXXXXXXXXXX");
        mDatabase.keepSynced(true);
        mPeopleRV = (RecyclerView) findViewById(R.id.myRecycleView);
        DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("XXXXXXXXXXX");
        Query personsQuery = personsRef.orderByChild("date");
        mPeopleRV.hasFixedSize();
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(Notification.this);
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);
        mPeopleRV.setLayoutManager(mLayoutManager);
        FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();
        mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
            @Override
            protected void onBindViewHolder(Notification.NewsViewHolder holder, final int position, final News model) {
                holder.setTitle(capitalize(model.getName()));
                //this click is not working 
                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                         String pos =  mPeopleRVAdapter.getRef(position).getKey();
                        Toast.makeText(getApplicationContext(), "dsdsdsd", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        mPeopleRV.setAdapter(mPeopleRVAdapter);
    }
    @Override
    public void onStart() {
        super.onStart();
        mPeopleRVAdapter.startListening();
    }
    @Override
    public void onStop() {
        super.onStop();
        mPeopleRVAdapter.stopListening();
    }
    public static class NewsViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public NewsViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title) {
            String output = title.substring(0, 1).toUpperCase() + title.substring(1);
            TextView post_title = (TextView) mView.findViewById(R.id.post_title);
            post_title.setText(output);
        }
    }
}

上述代码中我找不到的是什么问题,我试图从最近的4.5小时内修复此问题,当我需要该 firebase子对象 key 时,点击该项目

1 个答案:

答案 0 :(得分:0)

我一直在检查一些示例代码,我认为您缺少创建 onCreateViewHolder 方法并将其布局到适配器的方法。

代码:

DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("XXXXXXXXXXX");

        FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();

        mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
            //I think you are missing this part.
            @Override
            public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_message, parent, false);

                return new NewsViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(Notification.NewsViewHolder holder, final int position, final News model) {
                holder.setTitle(capitalize(model.getName()));
                //this click is not working
                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String pos =  mPeopleRVAdapter.getRef(position).getKey();
                        Toast.makeText(getApplicationContext(), "dsdsdsd", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

希望对您有帮助。