如何在列表视图中显示来自arraylist的所有解析数据?
Posted
技术标签:
【中文标题】如何在列表视图中显示来自arraylist的所有解析数据?【英文标题】:How to show all parse data from arraylist in list view? 【发布时间】:2017-11-13 16:44:54 【问题描述】:这是我的 JSON 数据
"products":
"617491704":
"user_id": "908",
"product_id": 9683,
"product": "Kishmish Raisins Sonaka 250 gm"
, "1405688942":
"user_id": "908",
"product_id": 9683,
"product": "Kishmish Raisins Sonaka 250 gm", "617491704":
"product_id": 9683,
"product": "Kishmish Raisins Sonaka 250 gm"
这是我解析 JSON 数据的代码
public static ArrayList<CartData> ParseData(String response) throws JSONException
ArrayList<CartData> aluser = null;
JSONObject jsonRoot = new JSONObject(response);
JSONObject jsonObject = jsonRoot.getJSONObject("products");
CartData details1 = new CartData();
aluser = new ArrayList<>();
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext())
String key = keys.next();
JSONObject inside = jsonObject.getJSONObject(key);
details1.setId(inside.getString("product_id"));
details1.setProductname(inside.getString("product"));
aluser.add(details1);
return aluser;
问题是,它只显示 ListView 中 arraylist 的最后一个值。请建议,我的代码中的问题在哪里。 这是我的 ListView 适配器。
private class AdapterCommentListcart extends BaseAdapter
private Context context;
private List<CartData> alComments;
private LayoutInflater inflater;
public AdapterCommentListcart(Context context, List<CartData> alComments)
this.context = context;
this.alComments = alComments;
inflater = LayoutInflater.from(this.context);
@Override
public View getView(int position, View convertView, ViewGroup parent)
AdapterCommentListcart.ViewHolder holder = null;
if (mapViewHolder.get(position) == null)
convertView = inflater.inflate(R.layout.products, null);
holder = initHolder(convertView, position);
attachEvents(holder, position);
convertView.setTag(holder);
mapViewHolder.put(position, convertView);
else
holder = (AdapterCommentListcart.ViewHolder) mapViewHolder.get(position).getTag();
updateHolder(holder, position);
return mapViewHolder.get(position);
@Override
public int getCount()
return alComments.size();
@Override
public CartData getItem(int position)
return alComments.get(position);
@Override
public long getItemId(int position)
return position;
private class ViewHolder
private TextView tvName;
private TextView tvprice;
private AdapterCommentListcart.ViewHolder initHolder(View convertView, int pos)
AdapterCommentListcart.ViewHolder holder = new AdapterCommentListcart.ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.id);
holder.tvprice = (TextView) convertView.findViewById(R.id.name);
return holder;
private void updateHolder(final AdapterCommentListcart.ViewHolder holder, final int pos)
holder.tvName.setText(alProduct.get(pos).getId());
holder.tvprice.setText(alProduct.get(pos).getProductname());
private void attachEvents(AdapterCommentListcart.ViewHolder holder, final int position)
【问题讨论】:
您是否尝试使用 JSONArray 而不是 JSONObject? 我建议你使用 Gson 进行 JSON 解析 【参考方案1】:将 CartData details1 放入 while 循环中。你只创建了一次对象,你应该在循环中每次都创建一个对象。
public static ArrayList<CartData> ParseData(String response) throws JSONException
ArrayList<CartData> aluser = new ArrayList<>();
JSONObject jsonRoot = new JSONObject(response);
if (jsonRoot == null)
return aluser;
JSONObject jsonObject = jsonRoot.optJSONObject("products");
if (jsonObject == null)
return aluser;
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext())
String key = keys.next();
if (TextUtils.isEmpty(key))
continue;
JSONObject inside = jsonObject.optJSONObject(key);
if (inside == null)
continue;
String productId = inside.optString("product_id");
String product = inside.optString("product");
if(TextUtils.isEmpty(product) || TextUtils.isEmpty(productId))
continue;
CartData details1 = new CartData();
details1.setId(productId);
details1.setProductname(product);
aluser.add(details1);
return aluser;
【讨论】:
【参考方案2】:只有问题是这条线
CartData details1 = new CartData();
它只创建一次对象,并且您每次都在 while 循环中添加同一个对象,因此不是只创建一次对象,而是每次在 while 循环中使用新数据创建它,而不是像下面这样添加。
public static ArrayList ParseData(String response) throws JSONException
ArrayList<CartData> aluser = null;
JSONObject jsonRoot = new JSONObject(response);
JSONObject jsonObject = jsonRoot.getJSONObject("products");
aluser = new ArrayList<>();
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext())
CartData details1 = new CartData();
String key = keys.next();
JSONObject inside = jsonObject.getJSONObject(key);
details1.setId(inside.getString("product_id"));
details1.setProductname(inside.getString("product"));
aluser.add(details1);
return aluser;
【讨论】:
与其他答案有什么不同..?以上是关于如何在列表视图中显示来自arraylist的所有解析数据?的主要内容,如果未能解决你的问题,请参考以下文章
不要在 ListFragment 中显示 Arraylist 的列表视图
从对象的 ArrayList 中获取字符串以用于自动完成文本视图