如何使用回收视图适配器将多个数据集作为对象加载?我尝试使用示例,但出现了与演员表相关的错误。有人可以帮忙吗? 这是我从中填充数据并调用适配器的活动的代码-
package com.example.aniketmondal.aniskitchen;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class OrderPage extends AppCompatActivity {
private FoodAdapter adapter;
private Query query;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_page);
setUpRecyclerView();
}
private void setUpRecyclerView() {
query = FirebaseDatabase.getInstance().getReference().child("/Food/Mughlai");
FirebaseRecyclerOptions<Food> options = new FirebaseRecyclerOptions.Builder<Food>().setQuery(query, Food.class).build();
adapter = new FoodAdapter(options);
RecyclerView rcview = findViewById(R.id.post_rv);
rcview.setHasFixedSize(true);
rcview.setLayoutManager(new LinearLayoutManager(this));
rcview.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
答案 0 :(得分:0)
您可以尝试在RecyclerView
方法上设置onCreate
。但是,最重要的是,您的适配器正在调用startListening()
方法,您没有从父类中重写该方法。因此,您可以将setUpRecyclerView()
重命名为startListening
。如果您在此处设置RecyclerView
,则无需从onCreate调用它。看起来像这样...
package com.example.aniketmondal.aniskitchen;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class OrderPage extends AppCompatActivity {
private FoodAdapter adapter;
private Query query;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_page);
RecyclerView rcview = findViewById(R.id.post_rv);
rcview.setHasFixedSize(true);
rcview.setLayoutManager(new LinearLayoutManager(this));
}
@Override
private void startListening() {
query = FirebaseDatabase.getInstance().getReference().child("/Food/Mughlai");
FirebaseRecyclerOptions<Food> options = new FirebaseRecyclerOptions.Builder<Food>().setQuery(query, Food.class).build();
adapter = new FoodAdapter(options);
rcview.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
答案 1 :(得分:0)
如果您已在onStart
中添加了侦听器,则必须在onStop
中将其删除。
如果您已在onResume
中添加了侦听器,则必须在onPause
中将其删除。
如果您已在onCreate
中添加了侦听器,则必须在onDestroy
中将其删除。但是请记住,并非总是调用onDestroy。