无JSON数组的对象如何在RecyclerView中展示?求技术指导
解决无JSON数组的对象在RecyclerView中展示的问题
1. 定义数据实体类
先创建一个实体类,用来存储单个货币的价格信息,方便后续RecyclerView适配:
public class CurrencyPrice { private String currencyCode; // 存储USD、EUR这类货币代码 private String min; private String max; private String current; // 构造方法 public CurrencyPrice(String currencyCode, String min, String max, String current) { this.currencyCode = currencyCode; this.min = min; this.max = max; this.current = current; } // Getter方法,供适配器调用获取数据 public String getCurrencyCode() { return currencyCode; } public String getCurrent() { return current; } public String getMin() { return min; } public String getMax() { return max; } }
2. 解析JSON并转换为列表
RecyclerView需要的是集合(比如List)而非键值对对象,所以核心是把prices里的键值对转成List<CurrencyPrice>,下面提供两种常用解析方式:
原生JSONObject解析
// 假设已获取完整的JSON字符串jsonString JSONObject rootObj = new JSONObject(jsonString); JSONObject dataObj = rootObj.getJSONObject("data"); JSONObject pricesObj = dataObj.getJSONObject("prices"); List<CurrencyPrice> priceList = new ArrayList<>(); // 遍历prices对象的所有货币键 Iterator<String> keys = pricesObj.keys(); while (keys.hasNext()) { String currencyCode = keys.next(); JSONObject currencyPriceObj = pricesObj.getJSONObject(currencyCode); String min = currencyPriceObj.getString("min"); String max = currencyPriceObj.getString("max"); String current = currencyPriceObj.getString("current"); priceList.add(new CurrencyPrice(currencyCode, min, max, current)); }
Gson库解析
先定义对应整个JSON结构的外层类:
public class PriceResponse { private Data data; public Data getData() { return data; } public static class Data { private String message; private int status; private Map<String, CurrencyDetail> prices; public Map<String, CurrencyDetail> getPrices() { return prices; } } public static class CurrencyDetail { private String min; private String max; private String current; // Getter方法 public String getMin() { return min; } public String getMax() { return max; } public String getCurrent() { return current; } } }
再解析并转成列表:
Gson gson = new Gson(); PriceResponse response = gson.fromJson(jsonString, PriceResponse.class); Map<String, PriceResponse.CurrencyDetail> priceMap = response.getData().getPrices(); List<CurrencyPrice> priceList = new ArrayList<>(); for (Map.Entry<String, PriceResponse.CurrencyDetail> entry : priceMap.entrySet()) { String currencyCode = entry.getKey(); PriceResponse.CurrencyDetail detail = entry.getValue(); priceList.add(new CurrencyPrice(currencyCode, detail.getMin(), detail.getMax(), detail.getCurrent())); }
3. 实现RecyclerView适配器
创建适配器,把列表数据绑定到单个条目布局:
public class PriceAdapter extends RecyclerView.Adapter<PriceAdapter.PriceViewHolder> { private List<CurrencyPrice> priceList; public PriceAdapter(List<CurrencyPrice> priceList) { this.priceList = priceList; } @NonNull @Override public PriceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_currency_price, parent, false); return new PriceViewHolder(view); } @Override public void onBindViewHolder(@NonNull PriceViewHolder holder, int position) { CurrencyPrice price = priceList.get(position); holder.tvCurrency.setText(price.getCurrencyCode()); holder.tvCurrentPrice.setText(price.getCurrent()); holder.tvMinPrice.setText("最低: " + price.getMin()); holder.tvMaxPrice.setText("最高: " + price.getMax()); } @Override public int getItemCount() { return priceList.size(); } static class PriceViewHolder extends RecyclerView.ViewHolder { TextView tvCurrency, tvCurrentPrice, tvMinPrice, tvMaxPrice; public PriceViewHolder(@NonNull View itemView) { super(itemView); tvCurrency = itemView.findViewById(R.id.tv_currency); tvCurrentPrice = itemView.findViewById(R.id.tv_current_price); tvMinPrice = itemView.findViewById(R.id.tv_min_price); tvMaxPrice = itemView.findViewById(R.id.tv_max_price); } } }
4. 配置RecyclerView并加载数据
在Activity/Fragment中完成RecyclerView的基础配置:
RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); PriceAdapter adapter = new PriceAdapter(priceList); recyclerView.setAdapter(adapter);
5. 单个条目布局示例
创建res/layout/item_currency_price.xml,定义每个货币条目的展示样式:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="vertical"> <TextView android:id="@+id/tv_currency" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold"/> <TextView android:id="@+id/tv_current_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_marginTop="4dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="4dp"> <TextView android:id="@+id/tv_min_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@android:color/darker_gray"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:text=" | " android:textColor="@android:color/darker_gray"/> <TextView android:id="@+id/tv_max_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@android:color/darker_gray"/> </LinearLayout> </LinearLayout>
内容的提问来源于stack exchange,提问作者developerspace null
相关产品推荐
相关产品推荐

