3 回答
TA贡献1877条经验 获得超1个赞
这样做的原因是您没有将 Double 转换为 String 将此行更改holder.rating.setText(rRatings.get(position));
为holder.rating.setText(String.valueOf(rRatings.get(position)));
TA贡献1841条经验 获得超3个赞
首先,您应该检查您获得的评级是否为双倍值。可能是在这一行之后抛出异常
mRating.add(resultsObj.getDouble("rating"))
同样,@hooray_todd siggested textview 只采用字符串值,因此您必须将数据更改为字符串。
TA贡献2012条经验 获得超12个赞
我建议您创建一个包含所有必要参数的对象,然后将该对象的单个列表传递给RecyclerViewAdapter.
public class MapElement {
public String name;
public String imageUrl;
public Double rating;
public Integer price;
}
然后ArrayList在您从getJson函数调用中获得响应时创建此对象的一个。现在在您的适配器中只需使用单个MapElement对象列表来显示那里的数据。
修改后的适配器应如下所示。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<MapElement> elements = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<MapElement> elements, Context context) {
this.elements = elements;
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
holder.name.setText(elements.get(position).name);
holder.rating.setText(elements.get(position).rating);
holder.priceLevel.setText(elements.get(position).price);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));
Toast.makeText(mContext, elements.get(position).name, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return elements.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
TextView rating;
TextView priceLevel;
TextView address;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
name = (TextView) itemView.findViewById(R.id.title);
rating = itemView.findViewById(R.id.rating);
priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
添加回答
举报