在RecyclerView中显示Firebase数据库项目中的多个项目

时间:2017-06-01 14:47:54

标签: android firebase firebase-realtime-database android-recyclerview

我正在尝试从我的firebase数据库中获取一个填充了信息的RecyclerView。

我只使用数据库中的一个项目(一个int),但现在我也添加了一个字符串,它将不再起作用。

我的数据库结构是

-random key id
  - item: "Coffee"
  - points: 10 

这是我的适配器

public class RecyclerViewAdapter extends 
    RecyclerView.Adapter<RecyclerViewHolders> {
    private List<Points> task;
    protected Context context;
    public RecyclerViewAdapter(Context context, List<Points> task) {
        this.task = task;
        this.context = context;
    }
    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int 
    viewType) {
        RecyclerViewHolders viewHolder = null;
        View layoutView = 
    LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, 
    parent, false);
        viewHolder = new RecyclerViewHolders(layoutView, task);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) 
    {
        holder.categoryTitle.setText((task.get(position).getPoints()));
        holder.itemTitle.setText(task.get(position).getItem());
    }
    @Override
    public int getItemCount() {
        return this.task.size();
    }
}

这是我的ViewHolder

public class RecyclerViewHolders extends RecyclerView.ViewHolder {
    private static final String TAG = 
    RecyclerViewHolders.class.getSimpleName();
    public ImageView markIcon;
    public TextView categoryTitle, itemTitle;
    public ImageView deleteIcon;
    private List<Points> taskObject;

    public RecyclerViewHolders(final View itemView, final List<Points> taskObject) {
        super(itemView);
        this.taskObject = taskObject;
        categoryTitle = (TextView) itemView.findViewById(R.id.task_title);
        itemTitle = (TextView) itemView.findViewById(R.id.itemTV);
    }
}

我的主要活动中的代码显示到列表

    allPoints = new ArrayList<Points>();
            recyclerView = (RecyclerView) 
    view.findViewById(R.id.point_list);
            linearLayoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(linearLayoutManager);

    private void getAllTask (DataSnapshot dataSnapshot) {
        for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
            String taskTitle = singleSnapshot.getValue(String.class);
            String taskItem = singleSnapshot.getValue(String.class);
            allPoints.add(new Points(taskItem, 
    Integer.parseInt(taskTitle)));
            recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), 
    allPoints);
            recyclerView.setAdapter(recyclerViewAdapter);
        }
    }

当我只显示'points'时,它正在工作,但现在我添加了String'item',它在该字符串上抛出了一个NumberFormatExepton。

  

java.lang.NumberFormatException:对于输入字符串:“Coffee”

这是我的Model类

public class Points {

    public int points;
    public String item;

    public Points(){}

    public Points(String item, int points) {
        this.points = points;
        this.item = item;
    }

    public int getPoints(){
        return points;
    }

    public void setPoints(int points){
        this.points = points;
    }

    public String getItem(){
        return item;
    }

    public void setItem(String item){
        this.item = item;
    }

}

这是我的结构

beans-card
 user
 urx53ou8uBhRODfyqqf57kb8Z0S2
 -KlYnqRvumMfu5h0FvLp
 item: 
   "Coffee"
 points: 
   10

和我的参考

 DatabaseReference db_receipt = 
 FirebaseDatabase.getInstance().getReference().child("user").child(mUserId);

2 个答案:

答案 0 :(得分:2)

1。)使用Points课程代替String

2.。)不要在循环的每次迭代中重新创建适配器

    for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
        Points point = singleSnapshot.getValue(Points.class);
        allPoints.add(point);
    }

// move adapter statements outside loop
recyclerViewAdapter = new RecyclerViewAdapter(getActivity(),allPoints);
recyclerView.setAdapter(recyclerViewAdapter);

答案 1 :(得分:0)

您使用了两次singleSnapshot.getValue(String.class);,因此您可以获得两次相同的数据。

您可以直接映射到您的对象。

Points point = singleSnapshot.getValue(Points.class);

然后,你在每个循环中不必要地创建一个新的适配器。你只需要一次。

recyclerView.setLayoutManager(linearLayoutManager);

// Move this under above line
recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), allPoints);
recyclerView.setAdapter(recyclerViewAdapter);

你可能想在循环之前清除列表。

private void getAllTask (DataSnapshot dataSnapshot) {
    allPoints.clear(); // Remove the old data
    for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
        allpoints.add(singleSnapshot.getValue(Points.class));
    }

    // Then, you notify the adapter, not create a new one
    recyclerViewAdapter.notifyDataSetChanged();
}