java - JSON对齐其循环相同的项目

标签 java android json

我在设置 json 数据时遇到问题它只设置了最后一项 4 次无法设置其他项目尝试了很多方法都找不到它请 friend 帮助这里

这是我正在使用的 JSON: http://www.yell4food.com/json/data_standard_item_new.php

这是java

public class Secondlevel extends Activity {

    List<JSONParser> itemsdata = new ArrayList<JSONParser>();
    String item, ids;
    ListView sec;
    Second_adapter secondAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlist);
        sec = (ListView) findViewById(R.id.seondlst);
        secondAdapter = new Second_adapter(Secondlevel.this, itemsdata);
        sec.setAdapter(secondAdapter);
        Intent hello = getIntent();
        item = hello.getStringExtra("name");
        new loaditems().execute();
    }
    public class loaditems extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... strings) {
            String url = "http://www.yell4food.com/json/data_standard_item_new.php";
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("rname","standardtakeaway"));
            params.add(new BasicNameValuePair("cname", item));
            params.add(new BasicNameValuePair("cids", ids));
            Calling calling = new Calling();
            String jurl = calling.makeHttpRequest(url, "GET", params);
            Log.d("dataurls", jurl);
            try {
                JSONArray array = new JSONArray(jurl);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject first = array.getJSONObject(i);
                    JSONParser parser = new JSONParser();
                    parser.setMenuname(first.getString("menu_name"));
                    JSONArray getitems = first.getJSONArray("items");
                    for (int j = 0; j < getitems.length(); j++) {
                        JSONObject sitems = getitems.getJSONObject(j);
                        parser.setIid(sitems.getInt("id"));
                        parser.setBaseName(sitems.getString("BaseName"));
                        parser.setItemdesc(sitems.getString("itemdesc"));
                        JSONArray subitems = sitems.getJSONArray("subitems");
                        for (int l = 0; l < subitems.length(); l++) {
                            JSONObject thrid = subitems.getJSONObject(l);
                            parser.setSid(thrid.getInt("id"));
                            parser.setSubItemdesc(thrid.getString("SubItemdesc"));
                            parser.setSubItemprice(thrid.getString("SubItemprice"));
                        }
                        itemsdata.add(parser);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Toast.makeText(getApplicationContext(), ""+item, Toast.LENGTH_SHORT).show();
            secondAdapter.notifyDataSetChanged();
        }
    }
}

这是适配器:

package com.app.prominere.standardtakeout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by prominere on 20-Oct-15.
 */
public class Second_adapter extends BaseAdapter {
    Context context;
    TextView basename,SubItemprice,itemdesc,SubItemdesc;
    LayoutInflater inflater;
    private List<JSONParser> items;

    public Second_adapter(Context context, List<JSONParser> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(view==null)
        view = inflater.inflate(R.layout.item_clicked, viewGroup, false);
        basename = (TextView) view.findViewById(R.id.basename);
        SubItemprice = (TextView) view.findViewById(R.id.SubItemprice);
        itemdesc = (TextView) view.findViewById(R.id.itemdesc);
        SubItemdesc = (TextView) view.findViewById(R.id.SubItemdesc);
        JSONParser setdata = items.get(i);
        basename.setText(setdata.getBaseName());
        itemdesc.setText(setdata.getItemdesc());
        SubItemdesc.setText(setdata.getSubItemdesc());
        SubItemprice.setText(setdata.getSubItemprice());
        return view;
    }
}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="BaseName"
        android:id="@+id/basename"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="SubItemdesc"
        android:id="@+id/SubItemdesc"
        android:layout_below="@+id/basename"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="SubItemprice"
        android:id="@+id/SubItemprice"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/basename"
        android:layout_margin="5dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="SubItemdesc"
        android:id="@+id/itemdesc"
        android:layout_below="@id/SubItemdesc"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/order"
        android:layout_margin="10dp"
        android:src="@drawable/order"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>
http://i.stack.imgur.com/SzhfK.png

最佳答案

JSONParser parser = new JSONParser(); 移到第二个 for 循环中。

你的代码将是这样的:

    JSONObject first = array.getJSONObject(i);
    JSONArray getitems = first.getJSONArray("items");
    for (int j = 0; j < getitems.length(); j++) {

         JSONParser parser = new JSONParser();
         parser.setMenuname(first.getString("menu_name"));

         JSONObject sitems = getitems.getJSONObject(j);
         parser.setIid(sitems.getInt("id"));
         parser.setBaseName(sitems.getString("BaseName"));
         parser.setItemdesc(sitems.getString("itemdesc"));
         JSONArray subitems = sitems.getJSONArray("subitems");

         for (int l = 0; l < subitems.length(); l++) {

              JSONObject thrid = subitems.getJSONObject(l);
              parser.setSid(thrid.getInt("id"));
              parser.setSubItemdesc(thrid.getString("SubItemdesc"));
              parser.setSubItemprice(thrid.getString("SubItemprice"));

         }

         itemsdata.add(parser);
    }

关于java - JSON对齐其循环相同的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652349/

相关文章:

json - json 模式中的对象重用和组合

java - 当 TextIO 完成写入文件时通知

java - Eclipse ide,无法解析新编写的函数中的变量

java - 获取 ANDROID_ID 试图了解 java

android - Android LVL 的实时测试

java - android - 如何在已经膨胀的布局中膨胀 map fragment ?

javascript - 使用 jquery 将数据绑定(bind)到 div 时在其中一列中添加链接

java - 使用Gson将Json转换为Java Object

java - 如何在 Java 中将 byte[] 存储在 Vector 中?

java - JBOSS 上的 apache cxf 客户端的 Web 服务请求 (>8KB) 失败 - HTTP 响应 '411: Length Required'