我正在创建我的第一个android studio应用,用户可以在其中发布日记条目。该发布有效(将其上传到Firebase的实时数据库中),我现在尝试使用回收者视图在卡片布局中的主活动页面中显示这些发布。可悲的是,当我运行我的应用程序时出现此错误。我会附上
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mDiaryList;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseRecyclerAdapter<Journal, JournalViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Journal");
mDiaryList = (RecyclerView) findViewById(R.id.diary_list);
mDiaryList.setLayoutManager(new LinearLayoutManager(this));
mDiaryList.setHasFixedSize(true);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Journal");
DatabaseReference personRef = FirebaseDatabase.getInstance().getReference().child("Journal");
Query query = personRef.orderByKey();
mDatabase.keepSynced(true);
FirebaseRecyclerOptions<Journal> options =
new FirebaseRecyclerOptions.Builder<Journal>()
.setQuery(query, Journal.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Journal, JournalViewHolder>(options) {
@NonNull
@Override
public JournalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.diary_list, parent, true);
return new JournalViewHolder(view);
}
//
@Override
protected void onBindViewHolder(@NonNull JournalViewHolder holder, int position, @NonNull Journal model) {
final String post_key = getRef(position).getKey().toString();
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
}
};
mDiaryList.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
//we need a viewholder to set up the Recycler view.
public static class JournalViewHolder extends RecyclerView.ViewHolder {
View mView;
public JournalViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// Red item was selected
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
return true;
case R.id.add_post:
//add_post was selected
startActivity(new Intent(MainActivity.this, PostActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
Journal.java
public class Journal {
private String title, desc, uid;
public Journal(String title, String desc, String uid) {
this.title = title;
this.desc = desc;
this.uid = uid;
}
public Journal() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
答案 0 :(得分:1)
您正在onCreate本地定义一个名为adapter
的变量,该变量在类级别遮盖了成员变量adapter
。您应该从分配中删除类型,以确保要分配给成员变量而不是局部变量:
// Remove "FirebaseRecyclerAdapter" from this line.
adapter = new FirebaseRecyclerAdapter<>(options) ...
答案 1 :(得分:1)
public class MainActivity extends AppCompatActivity {
private RecyclerView mDiaryList;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseRecyclerAdapter<Journal, JournalViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Journal");
mDiaryList = (RecyclerView) findViewById(R.id.diary_list);
mDiaryList.setLayoutManager(new LinearLayoutManager(this));
mDiaryList.setHasFixedSize(true);
//This is unnecessary.
mDatabase = FirebaseDatabase.getInstance().getReference().child("Journal");
//In my opinion this is unnecessary too. You already have a DatabaseReference object you declared as local variable.
DatabaseReference personRef = FirebaseDatabase.getInstance().getReference().child("Journal");
//Change this line to Query query = mDatabase.orderByKey();
Query query = personRef.orderByKey();
mDatabase.keepSynced(true);
FirebaseRecyclerOptions<Journal> options =
new FirebaseRecyclerOptions.Builder<Journal>()
.setQuery(query, Journal.class)
.build();
//Instantiate your adapter here
adapter = new FirebaseRecyclerAdapter<Journal, JournalViewHolder>(options) {
@NonNull
@Override
public JournalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.diary_list, parent, true);
return new JournalViewHolder(view);
}
//
@Override
protected void onBindViewHolder(@NonNull JournalViewHolder holder, int position, @NonNull Journal model) {
final String post_key = getRef(position).getKey().toString();
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
}
};
mDiaryList.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
//we need a viewholder to set up the Recycler view.
public static class JournalViewHolder extends RecyclerView.ViewHolder {
View mView;
public JournalViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// Red item was selected
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
return true;
case R.id.add_post:
//add_post was selected
startActivity(new Intent(MainActivity.this, PostActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}