PARAG PATHE PARAG PATHE -4 years ago 75
Android Question

How to show all parse data from arraylist in list view?

This is my JSON data

{ "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"
}}


This is my code to parse this JSON data

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


The problem is, it shows only last value of arraylist in ListView. Please suggest, where is the problem in my code.
This is my Adapter for 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) {

}
}

Answer Source

Put CartData details1 inside while loop. you created an object only one time, you should create an object every time in the loop.

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;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download