安卓火库回收器适配器单击项目不起作用



我在我的安卓项目中显示了来自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 子对象的

我一直在检查一些示例代码,我认为您缺少创建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();
}
});
}
};

我希望它对你有所帮助。

相关内容

  • 没有找到相关文章

最新更新