猿问

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

当我只显示字符串数据时,它似乎显示正常,但是当我尝试显示 int 或 double 时,RecyclerView 不会填充。我从 Google 地方 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班级是。


红颜莎娜
浏览 226回答 3
3回答

冉冉说

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

偶然的你

首先,您应该检查您获得的评级是否为双倍值。可能是在这一行之后抛出异常mRating.add(resultsObj.getDouble("rating"))同样,@hooray_todd siggested textview 只采用字符串值,因此您必须将数据更改为字符串。

繁花如伊

我建议您创建一个包含所有必要参数的对象,然后将该对象的单个列表传递给RecyclerViewAdapter.public class MapElement {&nbsp; &nbsp; public String name;&nbsp; &nbsp; public String imageUrl;&nbsp; &nbsp; public Double rating;&nbsp; &nbsp; public Integer price;}然后ArrayList在您从getJson函数调用中获得响应时创建此对象的一个。现在在您的适配器中只需使用单个MapElement对象列表来显示那里的数据。修改后的适配器应如下所示。public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{&nbsp; &nbsp; private static final String TAG = "RecyclerViewAdapter";&nbsp; &nbsp; private ArrayList<MapElement> elements = new ArrayList<>();&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; public RecyclerViewAdapter(ArrayList<MapElement> elements, Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this.elements = elements;&nbsp; &nbsp; &nbsp; &nbsp; mContext = context;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; ViewHolder holder = new ViewHolder(view);&nbsp; &nbsp; &nbsp; &nbsp; return holder;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {&nbsp; &nbsp; &nbsp; &nbsp; Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);&nbsp; &nbsp; &nbsp; &nbsp; holder.name.setText(elements.get(position).name);&nbsp; &nbsp; &nbsp; &nbsp; holder.rating.setText(elements.get(position).rating);&nbsp; &nbsp; &nbsp; &nbsp; holder.priceLevel.setText(elements.get(position).price);&nbsp; &nbsp; &nbsp; &nbsp; holder.parentLayout.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, elements.get(position).name, Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return elements.size();&nbsp; &nbsp; }&nbsp; &nbsp; public class ViewHolder extends RecyclerView.ViewHolder{&nbsp; &nbsp; &nbsp; &nbsp; ImageView image;&nbsp; &nbsp; &nbsp; &nbsp; TextView name;&nbsp; &nbsp; &nbsp; &nbsp; TextView rating;&nbsp; &nbsp; &nbsp; &nbsp; TextView priceLevel;&nbsp; &nbsp; &nbsp; &nbsp; TextView address;&nbsp; &nbsp; &nbsp; &nbsp; RelativeLayout parentLayout;&nbsp; &nbsp; &nbsp; &nbsp; public ViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = (ImageView) itemView.findViewById(R.id.image);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = (TextView) itemView.findViewById(R.id.title);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rating = itemView.findViewById(R.id.rating);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentLayout = itemView.findViewById(R.id.parentLayout);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答