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

Java 如何在列表视图中显示arraylist中的所有解析数据?,java,android,json,listview,arraylist,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"

这是我的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"
}}
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;
    }
这是我解析这个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"
}}
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;
    }
publicstaticarraylistparsedata(字符串响应)抛出JSONException{
ArrayList aluser=null;
JSONObject jsonRoot=新的JSONObject(响应);
JSONObject JSONObject=jsonRoot.getJSONObject(“产品”);
CartData details1=新的CartData();
aluser=newarraylist();
迭代器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(“产品”);
增加(详情1);
}
返回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) {

    }
}
私有类适配器CommentListCart扩展BaseAdapter{
私人语境;
私人名单;
私人充气机;
公共适配器CommentListCart(上下文、列表注释){
this.context=上下文;
this.alComments=alComments;
充气器=从(此上下文)开始的充气器;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
AdapterCommentListcart.ViewHolder=null;
if(mapViewHolder.get(position)==null){
convertView=充气机充气(R.layout.products,空);
保持架=初始保持架(转换视图,位置);
附件通风口(支架、位置);
convertView.setTag(支架);
mapViewHolder.put(位置,转换视图);
}否则{
holder=(AdapterCommentListcart.ViewHolder)mapViewHolder.get(position.getTag();
}
更新文件夹(支架、位置);
返回mapViewHolder.get(位置);
}
@凌驾
public int getCount()
{
返回alComments.size();
}
@凌驾
公共数据获取项(int位置){
返回alComments.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
私有类视窗持有者{
私有文本视图tvName;
私有文本视图价格;
}
专用适配器CommentListCart.ViewHolder initHolder(视图转换视图,int pos){
AdapterCommentListcart.ViewHolder=new AdapterCommentListcart.ViewHolder();
holder.tvName=(TextView)convertView.findViewById(R.id.id);
holder.tvprice=(TextView)convertView.findViewById(R.id.name);
报税表持有人;
}
私有无效更新文件夹(最终适配器CommentListCart.ViewHolder holder,最终int pos){
holder.tvName.setText(alProduct.get(pos.getId());
holder.tvprice.setText(alProduct.get(pos.getProductname());
}
私有无效附件事件(AdapterCommentListcart.ViewHolder支架,最终int位置){
}
}

在while循环中放入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;
}
publicstaticarraylistparsedata(字符串响应)抛出JSONException{
ArrayList aluser=新的ArrayList();
JSONObject jsonRoot=新的JSONObject(响应);
if(jsonRoot==null){
返回aluser;
}
JSONObject JSONObject=jsonRoot.optJSONObject(“产品”);
if(jsonObject==null){
返回aluser;
}
迭代器keys=jsonObject.keys();
while(keys.hasNext()){
String key=keys.next();
if(TextUtils.isEmpty(键)){
继续;
}
JSONObject inside=JSONObject.optJSONObject(key);
如果(内部==null){
继续;
}
String productId=inside.optString(“product_id”);
字符串product=inside.optString(“产品”);
if(TextUtils.isEmpty(产品)| | TextUtils.isEmpty(产品ID)){
继续;
}
CartData details1=新的CartData();
详细信息1.setId(productId);
详细信息1.setProductname(产品);
增加(详情1);
}
返回aluser;
}

唯一的问题是这一行

 CartData details1 = new CartData();
它只创建了一次对象,而您每次都在while循环中添加了相同的对象,因此不是每次只创建一次对象,而是每次在while循环中使用新数据创建对象,然后像下面这样添加。

公共静态ArrayList ParseData(字符串响应)抛出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 aluser=null;
JSONObject jsonRoot=新的JSONObject(响应);
JSONObject JSONObject=jsonRoot.getJSONObject(“产品”);
aluser=newarraylist();
迭代器keys=jsonObject.keys();
while(keys.hasNext()){
CartData details1=新的CartData();
String key=keys.next();
JSONObject inside=JSONObject.getJSONObject(key);
details1.setId(inside.getString(“product_id”));
details1.setProductname(inside.getString(“产品”);
增加(详情1);
}
返回aluser;
}

您是否尝试使用JSONArray而不是JSONObject?我建议您使用Gson进行JSON解析与其他答案有什么不同。。?