如何在列表视图中显示arraylist中的所有解析数据?

时间:2017-06-12 09:08:05

标签: java android json listview arraylist

这是我的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) {

    }
}

2 个答案:

答案 0 :(得分:2)

将循环中的CartData details1放入其中。你只创建了一次对象,你应该每次在循环中创建一个对象。

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;
}

答案 1 :(得分:1)

只有问题就在这一行

 CartData details1 = new CartData();

它只创建一次对象,并且每次在循环中都添加了这个相同的对象,所以不是创建对象,而是每次创建对象时都会在循环中创建新数据,而不是像下面那样添加。

public static ArrayList ParseData(String response)抛出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;
}