由于某种原因,on bindviewholder不接受Viewholder创建的变量(holder.name和holder.dexription)。我已经尽力修复了所有问题,但没有任何效果。如果可以帮助,请看下面的代码。
public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<City> cities;
public CityAdapter(ArrayList<City> cities) {
this.cities =cities;
}
@Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
View v =(View) LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
City city = cities.get(position);
holder.name.setText(city.getName());
holder.description.setText(city.getDescription());
}
static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView name;
public TextView description;
public ImageView image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.name=(TextView) itemView.findViewById(R.id.name);
this.description=(TextView) itemView.findViewById(R.id.description);
this.image=(ImageView) itemView.findViewById(R.id.image);
}
}
}
答案 0 :(得分:4)
您来自错误的班级
更改}
Gson gson = new Gson();
Data data = gson.fromJson(json, Data.class); System.out.println(data.getRegional().get("asia-pacific-east"));
并从以下位置更改“ onBindViewHolder”方法的第一个参数
CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
到
CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>