这是我的代码:
当我运行此代码时,您的应用已停止显示错误。
Android Studio中未显示错误,只有应用程序崩溃。
我想我正在设置参考。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class FoodActivity extends AppCompatActivity {
private DatabaseReference mDatabaseRef;
private RecyclerView mRecycler;
private FirebaseDatabase mFirebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
String data = getIntent().getStringExtra("data");
mFirebaseDatabase = FirebaseDatabase.getInstance();
Toast.makeText(this, "select "+data, Toast.LENGTH_SHORT).show();
mDatabaseRef = mFirebaseDatabase.getReference().child("item/1");
mRecycler=(RecyclerView) findViewById(R.id.listViewFood);
mRecycler.setHasFixedSize(true);
mRecycler.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Food,foodViewHolder> mFirebaseAdapter=new FirebaseRecyclerAdapter<Food, foodViewHolder>(
Food.class,R.layout.food_list,foodViewHolder.class,mDatabaseRef
) {
@Override
protected void populateViewHolder(foodViewHolder viewHolder, Food model, int position) {
viewHolder.setfname(model.getfname());
viewHolder.setPrice(model.getPrice());
}
};
mRecycler.setAdapter(mFirebaseAdapter);
}
public static class foodViewHolder extends RecyclerView.ViewHolder{
View mView;
public foodViewHolder(View itemView) {
super(itemView);
mView= itemView;
}
public void setfname(String fname){
TextView tvFood=(TextView) mView.findViewById(R.id.tvFood);
tvFood.setText(fname);
}
public void setPrice(int price){
TextView tvprice=(TextView) mView.findViewById(R.id.fprice);
tvprice.setText(String.valueOf(price));
}
}
}
答案 0 :(得分:0)
请务必在onStart()
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
它做什么
FirebaseRecyclerAdapter使用事件侦听器来监视更改 到Firebase查询。要开始收听数据,请致电 startListening()方法。您可能想在onStart()中调用它 方法。确保您已完成所需的任何身份验证 在调用startListening()之前读取数据,否则您的查询将失败。
并在onStop();
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
Similarly, the stopListening() call removes the event listener and all data in the adapter. Call this method when the containing Activity or Fragment stops
编辑:使您的适配器全局
private FirebaseRecyclerAdapter<Food,foodViewHolder> mFirebaseAdapter;
然后在onStart();
更改此
FirebaseRecyclerAdapter<Food,foodViewHolder> mFirebaseAdapter=new FirebaseRecyclerAdapter<Food, foodViewHolder>(
Food.class,R.layout.food_list,foodViewHolder.class,mDatabaseRef....
到这个
mFirebaseAdapter=new FirebaseRecyclerAdapter<Food, foodViewHolder>(
Food.class,R.layout.food_list,foodViewHolder.class,mDatabaseRef...
希望有所帮助
快乐的编码!