解析json后无法在incyclerview上显示int或double

时间:2018-10-01 03:15:24

标签: java android android-recyclerview android-adapter

当我只显示字符串数据时,看起来似乎很好,但是当我尝试显示int或double时,不会填充RecyclerView。我从Google Places API JSON中提取。我认为也许是我使用onBindViewHolder()的方式吗?谢谢

// MainActivity
private void getJson() {

    String URL = https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7085,-74.003124&opennow&radius=5000&type=restaurant&key=

    JsonObjectRequest root = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray results = response.getJSONArray("results");
                Log.d("RESULTS", String.valueOf(results));
                for(int i = 0; i<results.length(); i++) {
                    JSONObject resultsObj = results.getJSONObject(i);

                    mImageUrls.add(resultsObj.getString("icon"));
                    mTitle.add(resultsObj.getString("name"));
                    //mRating.add(resultsObj.getDouble("rating"));
                    mPriceLevel.add(resultsObj.getInt("price_level"));
                }
                getRecyclerView();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    mRequestQueue.add(root);
}

private void getRecyclerView() {
    // LinearLayoutManager
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new RecyclerViewAdapter(mImageUrls, mTitle, mPriceLevel, this);

    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
}

RecyclerViewAdapter类是

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

    private static final String TAG = "RecyclerViewAdapter";

    private ArrayList<String> rNames = new ArrayList<>();
    private ArrayList<String> rImageUrls = new ArrayList<>();
    private ArrayList<Double> rRatings = new ArrayList<>();
    private ArrayList<Integer> rPriceLevel = new ArrayList<>();

    private Context mContext;

    public RecyclerViewAdapter(ArrayList<String> image, ArrayList<String> name, ArrayList<Integer> priceLevel, Context context) {//, ArrayList<Double> rating
        this.rImageUrls = image;
        this.rNames = name;
        //this.rRatings = rating;
        this.rPriceLevel = priceLevel;
        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;
        //return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
        holder.name.setText(rNames.get(position));
        //holder.rating.setText(rRatings.get(position));
        holder.priceLevel.setText(rPriceLevel.get(position));

        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, rNames.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return rNames.size(); //we can use rImageUrls/rRatings as well because they are all the same 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);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

原因是您没有将Double转换为String 将此行holder.rating.setText(rRatings.get(position));更改为holder.rating.setText(String.valueOf(rRatings.get(position)));

答案 1 :(得分:0)

我建议您创建一个包含所有必需参数的对象,然后将该对象的单个列表传递给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);
        }
    }
}

答案 2 :(得分:0)

首先,您应该检查所获得的评级是否为双精度值。 可能是在此行

之后引发了异常
mRating.add(resultsObj.getDouble("rating"))

此外@hooray_todd带有Siggested的textview仅采用字符串值,因此您必须将数据更改为字符串。