Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView:通过json数组循环到列表_Android - Fatal编程技术网

Android ListView:通过json数组循环到列表

Android ListView:通过json数组循环到列表,android,Android,这方面的工作已经进行了几个小时了,但我并不确定应该如何对我的测试数组进行排序。我已经尝试了一些不同的选项,这是我的项目中最简单的实现方法。有什么建议或想法可以帮助我克服这个困难吗 来自php的JSON数组 { "Questions": { "0001": { "Title": "What is Stackoverflow", "Answer": "C", "User": "testUSER",

这方面的工作已经进行了几个小时了,但我并不确定应该如何对我的测试数组进行排序。我已经尝试了一些不同的选项,这是我的项目中最简单的实现方法。有什么建议或想法可以帮助我克服这个困难吗

来自php的JSON数组

{
    "Questions": {
        "0001": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSER",
            "Date": "0000-00-00",
            "Used": "0"
        },
        "0002": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSERb",
            "Date": "0000-00-00",
            "Used": "1"
        },
        "0003": {
            "Title": "What is Stackoverflow",
            "Answer": "C",
            "User": "testUSERc",
            "Date": "0000-00-00",
            "Used": "0"
        }
    },
    "Count-start": 1,
    "Count-end": 3
}
已使用:0=未使用1=用户已使用。我将使用它来确定是否向用户显示问题和答案

助手类

public JSONObject query(){
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
        HttpResponse response;
        JSONObject json = new JSONObject();
        HttpPost post = new HttpPost(QUERY_LINK);
        post.setHeader("json", json.toString());
        StringEntity se;
        try {
            se = new StringEntity(json.toString());
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
            post.setEntity(se);
            response = client.execute(post);
            if (response != null) {
                InputStream in = response.getEntity().getContent();
                a = convertStreamToString(in);
                JSONObject check = new JSONObject(a);   
                return check;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
主要活动

private ArrayList<ListHelper> GetSearchResults(){
        JSONObject getJSON = new Helper().query();
        JSONArray jArrayObject = new JSONArray();
        jArrayObject.put(getJSON);
        int end = 0,start = 0;
        String title, answer, user, used;
        ListHelper sr = new ListHelper();


     ArrayList<ListHelper> results = new ArrayList<ListHelper>();
     JSONObject offerObject = null;
        try {
            offerObject = getJSON.getJSONObject("Questions");
            start = offerObject.getInt("Count-start");
            end = offerObject.getInt("Count-end");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


     for(int a = start; a < end; a = a++) {
         try {


            JSONObject businessObject = offerObject.getJSONObject("Id");
            // this is were i'm stuck, how to go about selecting each id

            title = businessObject.getString("Title");
            answer = businessObject.getString("Answer");
            user = businessObject.getString("User");
            used = businessObject.getString("Used");

            sr = new ListHelper();
             sr.setTitle(title);
             sr.setUser(user);
             sr.setUsed(used);
             results.add(sr);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
      }
     return results;
    }
}
private ArrayList GetSearchResults(){
JSONObject getJSON=new Helper().query();
JSONArray jArrayObject=新的JSONArray();
jArrayObject.put(getJSON);
int end=0,start=0;
字符串标题、答案、用户、已使用;
ListHelper sr=新的ListHelper();
ArrayList结果=新建ArrayList();
JSONObject offerObject=null;
试一试{
offerObject=getJSON.getJSONObject(“问题”);
start=offerObject.getInt(“计数开始”);
end=offerObject.getInt(“计数结束”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
for(int a=start;a
您可以执行以下操作:在
for
循环之外定义一个新的
十进制格式

DecimalFormat myFormatter = new DecimalFormat("0000"); 
然后在
for
循环中使用格式化程序形成
id
字符串

String id = myFormatter.format(a);
然后使用
id
获取
JSONObject

// this is were i'm stuck, how to go about selecting each id
JSONObject businessObject = offerObject.getJSONObject(id);
仅当您的
ID
最多为4位时,上述操作才有效。您可以使用
“Count end”
值而不是使用硬编码的
“0000”
使格式动态


如果
“Questions”
对象是一个JSONArray,那么所有这些问题都可以避免。

来自php的Json数据不是Json数组类型。。它完全是Json对象。