我一直在测试如何将Firebase Realtime数据库中的数据实现到我的recyclerviews中。
我在oncreate中添加了Firebasedatabase.getInstance()。getReference。但是它似乎没有为我提供我需要输入到recyclerview的数据
import android.os.Bundle;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import sg.edu.tp.soundup.Adapters.RecyclerViewDataAdapter;
import sg.edu.tp.soundup.Models.SectionDataModel;
import sg.edu.tp.soundup.Models.SingleItemModel;
public class Main_act extends AppCompatActivity {
private static final String TAG = "Main_act";
private ArrayList<SectionDataModel> listItem;
DatabaseReference Data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_act);
listItem = new ArrayList<>();
Data = FirebaseDatabase.getInstance().getReference("Data");
createDummyData();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerViewDataAdapter adapter = new RecyclerViewDataAdapter(listItem, this);
recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
private void createDummyData() {
for (int i = 1; i <= 5; i++) {
SectionDataModel dm = new SectionDataModel();
dm.setHeaderTitle("Section " + i);
ArrayList<SingleItemModel> singleItemModels = new ArrayList<>();
for (int j = 1; j <= 5; j++) {
singleItemModels.add(new SingleItemModel("Item " + j, "art " + j));
}
dm.setAllItemInSection(singleItemModels);
listItem.add(dm);
}
}
}
public class SingleItemModel {
public String name, art;
public SingleItemModel() {
}
public SingleItemModel(String name, String art) {
this.name = name;
this.art = art;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getart() {
return art;
}
public void setart(String art) {
this.art = art;
}
}
import java.util.ArrayList;
public class SectionDataModel {
private String headerTitle;
private ArrayList<SingleItemModel> listItem;
public SectionDataModel() {
}
public SectionDataModel(String headerTitle, ArrayList<SingleItemModel> listItem) {
this.headerTitle = headerTitle;
this.listItem = listItem;
}
public String getHeaderTitle() {
return headerTitle;
}
public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}
public ArrayList<SingleItemModel> getAllItemInSection() {
return listItem;
}
public void setAllItemInSection(ArrayList<SingleItemModel> allItemInSection) {
this.listItem = allItemInSection;
}
}
public class SectionListDataAdapter extends
RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder>{
private ArrayList<SingleItemModel> itemModels;
private Context mContext;
public SectionListDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) {
this.itemModels = itemModels;
this.mContext = mContext;
}
@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v);
return singleItemRowHolder;
}
@Override
public void onBindViewHolder(SingleItemRowHolder holder, int position) {
SingleItemModel itemModel = itemModels.get(position);
holder.tvTitle.setText(itemModel.getName());
}
@Override
public int getItemCount() {
return (null != itemModels ? itemModels.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageButton itemImage;
public SingleItemRowHolder(View itemView) {
super(itemView);
this.tvTitle = itemView.findViewById(R.id.tvTitle);
this.itemImage = itemView.findViewById(R.id.itemImage);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
公共类RecyclerViewDataAdapter扩展了RecyclerView.Adapter {
private ArrayList<SectionDataModel> dataList;
private Context mContext;
private RecyclerView.RecycledViewPool recycledViewPool;
public RecyclerViewDataAdapter(ArrayList<SectionDataModel> dataList, Context mContext) {
this.dataList = dataList;
this.mContext = mContext;
recycledViewPool = new RecyclerView.RecycledViewPool();
}
@Override
public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder rowHolder = new ItemRowHolder(v);
return rowHolder;
}
@Override
public void onBindViewHolder(ItemRowHolder holder, int position) {
final String sectionName = dataList.get(position).getHeaderTitle();
ArrayList singleSectionItems = dataList.get(position).getAllItemInSection();
holder.itemTitle.setText(sectionName);
SectionListDataAdapter adapter = new SectionListDataAdapter(singleSectionItems, mContext);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setRecycledViewPool(recycledViewPool);
}
@Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView itemTitle;
protected RecyclerView recyclerView;
public ItemRowHolder(View itemView) {
super(itemView);
this.itemTitle = itemView.findViewById(R.id.itemTitle);
this.recyclerView = itemView.findViewById(R.id.recycler_view_list);
}
}
}
我希望能够从Firebase接收数据并将其存储在recyclerviews中。