我試圖從我的Firebase數據庫中獲取充滿信息的RecyclerView。在RecyclerView中顯示Firebase數據庫項目中的多個項目
我曾在數據庫中只有一個項目(一個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);
}
}
它正在工作,當我只顯示'點',但現在我添加了字符串'項'它是在該字符串上拋出一個NumberFormatExepton。
java.lang.NumberFormatException:對於輸入字符串: 「咖啡」
這裏是我的模型類以及
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);
'對於輸入字符串:「咖啡」'...那麼不要存儲:'咖啡'在那個變量 –
那麼,你認爲「咖啡」是一個有效的數字嗎? –