我在设置json数据时遇到问题,只设置最后一项4次无法设置其他项目尝试了很多方法无法找到它请朋友们帮忙
这是我正在使用的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
答案 0 :(得分:1)
将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);
}
答案 1 :(得分:0)
这个答案并没有专门解答这个问题,但可以用来解决几个Gson和Json反序列化问题。
这些代码可以使用Gson库轻松使用,同时填充对象。这可以很容易地在代码中使用。
创建一个包含菜单项的Response类
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("menu_name")
private String menuName;
@SerializedName("items")
private ArrayList<Items> items;
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public ArrayList<Items> getItems() {
return items;
}
public void setItems(ArrayList<Items> items) {
this.items = items;
}
}
Items.class
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Items {
@SerializedName("id")
private String id;
@SerializedName("BaseName")
private String baseName;
@SerializedName("itemdesc")
private String itemdesc;
@SerializedName("subitems")
private ArrayList<SubItem> subItems;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBaseName() {
return baseName;
}
public void setBaseName(String baseName) {
this.baseName = baseName;
}
public String getItemdesc() {
return itemdesc;
}
public void setItemdesc(String itemdesc) {
this.itemdesc = itemdesc;
}
public ArrayList<SubItem> getSubItems() {
return subItems;
}
public void setSubItems(ArrayList<SubItem> subItems) {
this.subItems = subItems;
}
}
SubItem.class
import com.google.gson.annotations.SerializedName;
public class SubItem {
@SerializedName("id")
private String id;
@SerializedName("SubItemdesc")
private String subItemdesc;
@SerializedName("SubItemprice")
private String subItemprice;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubItemdesc() {
return subItemdesc;
}
public void setSubItemdesc(String subItemdesc) {
this.subItemdesc = subItemdesc;
}
public String getSubItemprice() {
return subItemprice;
}
public void setSubItemprice(String subItemprice) {
this.subItemprice = subItemprice;
}
}
您可以使用以下类来读取整个数据并使用Gson库
填充对象import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class MyTest {
public static void main(String[] args) {
String stringurl = "http://www.yell4food.com/json/data_standard_item_new.php";
try {
URL url = new URL(stringurl);
URLConnection connection = url.openConnection();
connection.connect();
String result = IOUtils.toString(connection.getInputStream());
System.out.println("" + result);
ArrayList<Response> responses = (new Gson()).fromJson(result,
new TypeToken<ArrayList<Response>>() {
}.getType());
for (Response response : responses) {
System.out.println("" + response.getMenuName());
ArrayList<Items> items = response.getItems();
if (items != null) {
if (!items.isEmpty()) {
for (Items items2 : items) {
System.out.println("\t"+items2.getId());
System.out.println("\t"+items2.getBaseName());
System.out.println("\t"+items2.getItemdesc());
if (items3 != null) {
if (!items3.isEmpty()) {
for (SubItem subItem : items3) {
System.out.println("\t\t"+subItem.getId());
System.out.println("\t\t"+subItem.getSubItemdesc());
System.out.println("\t\t"+subItem.getSubItemprice());
}
}
}
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
此示例使用两个库commons-io-2.4.jar
和Gson
Beverages
1
Coca-Cola
1
0.33L
0.75
2
1.5L
1.85
2
Diet Coca-Cola
1
0.33L
0.75
2
1.5L
1.85