如何循环JSON数组和列表视图打印值

如何循环JSON数组和列表视图打印值

本文介绍了如何循环JSON数组和列表视图打印值? ANDROID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的使用REST API's.This工作是JSON响应我得到,
有谁能够告诉我如何循环这个JSON阵列打印关键字和放大器;在ListView CREATION_DATE?

  {结果:OK,
关键词:
    [
    {keyword_id:3,
     关键词:关键字
     CREATION_DATE:2015年3月12日15点45分12秒,
     描述:,
     标签:,
     FOLLOWERS_COUNT:0,
     feedbacks_count:0
     },
     {keyword_id:4,
      关键词:关键字2,
      CREATION_DATE:2015年3月12日十五时45分34秒,
      描述:,
      标签:,
      FOLLOWERS_COUNT:0,
      feedbacks_count:0
     }
     ]
      错误:
}

这是我要怎样做。

 保护无效onPostExecute(字符串结果){
            Toast.makeText(getBaseContext(),结果,Toast.LENGTH_LONG).show();
            关键字=新的ArrayList< FollowingKeywords>();
            关键字清单=新的ArrayList<串GT;();
            尝试{
                JSONObject的JSON =新的JSONObject(结果);
                字符串结果= json.getString(结果);                如果(Result.equals(OK)){
                    JSONArray jarray =新JSONArray(关键词);
                    的for(int i = 0; I< jarray.length();我++){                        FollowingKeywords关键字=新FollowingKeywords();
                        keywords.setKeyword(JSONObject.optString(关键字));
                        keywords.setTimestamp(JSONObject.optString(CREATION_DATE));
                        keyWord.add(关键字);                    }
                    数= jarray.length();
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
        }


解决方案

使用 android.R.layout.simple_list_item_2 作为连续项的的ListView 。您需要创建的自定义适配器的,以同时显示在的ListView 的文字。坐落于这两个文本getView()方法。另外,使用平滑滚动 ViewHolder 图案的ListView

 保护无效onPostExecute(字符串结果){
        JSONObject的JSON = NULL;
        尝试{
            JSON =新的JSONObject(结果);
        }赶上(JSONException E){
            e.printStackTrace();
        }        ArrayList的< FollowingKeywords>关键字=新的ArrayList< FollowingKeywords>();
        JSONArray keywordArray = json.optJSONArray(关键词);
        如果(keywordArray!= NULL){
            的for(int i = 0; I< keywordArray.length();我++){
                JSONObject的keywordObject = keywordArray.optJSONObject(I)
                如果(keywordObject!= NULL){
                     FollowingKeywords关键字=新FollowingKeywords();
                     keywords.setKeyword(keywordObject.optString(关键字));
                     keywords.setTimestamp(keywordObject.optString(CREATION_DATE));
                     keyWord.add(关键字);
                }
            }
        }        MyAdapter myAdapter =新MyAdapter(这一点,android.R.layout.simple_list_item_2,关键词);
        ListView控件ListView1的=(的ListView)findViewById(R.id.listView1);
        listView1.setAdapter(myAdapter);
    }

MyAdapter.java

 公共类MyAdapter扩展ArrayAdapter< FollowingKeywords> {    上下文语境;
    清单< FollowingKeywords>关键字清单;    公共MyAdapter(上下文的背景下,INT资源的ArrayList< FollowingKeywords>的keywordlist){
        超级(上下文,资源,关键字列表);
        this.context =背景;
        this.keywordList =关键字清单;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看排= convertView;
        ViewHolder持有人;
        如果(行== NULL){
            LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            行=(查看)inflater.inflate(android.R.layout.simple_list_item_2,NULL);            持有人=新ViewHolder();
            holder.keywordText =(TextView中)row.findViewById(android.R.id.text1);
            holder.creationText =(TextView中)row.findViewById(android.R.id.text2);            row.setTag(保持器);
        }其他{
            支架=(ViewHolder)row.getTag();
        }        holder.keywordText.setText(keywordList.get(位置).getKeyword());
        holder.creationText.setText(keywordList.get(位置).getTimestamp());        返回行;
    }    静态类ViewHolder {
        TextView的keywordText;
        TextView的creationText;
    }}

I am quite new working with REST API's.This is the JSON Response i am getting,can anybody please tell me how to loop this JSON Array printing keyword & Creation_date in a listView?

{"result": "ok",
"keywords":
    [
    {"keyword_id":"3",
     "keyword":"keyword1",
     "creation_date":"2015­-03-­12 15:45:12",
     "description":"",
     "tags":"­",
     "followers_count":0,
     "feedbacks_count":0
     },
     {"keyword_id":"4",
      "keyword":"keyword2",
      "creation_date":"2015-03-­12 15:45:34",
      "description":"",
      "tags":"­",
      "followers_count":0,
      "feedbacks_count":0
     }
     ],
      "error":""
}

This is what i was trying to do.

protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
            keyWord = new ArrayList<FollowingKeywords>();
            keywordlist = new ArrayList<String>();
            try {
                JSONObject json = new JSONObject(result);
                String Result = json.getString("result");

                if (Result.equals("ok")) {
                    JSONArray jarray = new JSONArray("keywords");
                    for (int i = 0; i < jarray.length(); i++) {

                        FollowingKeywords keywords = new FollowingKeywords();
                        keywords.setKeyword(JSONObject.optString("keyword"));
                        keywords.setTimestamp(JSONObject.optString("creation_date"));
                        keyWord.add(keywords);

                    }
                    count = jarray.length();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
解决方案

Use android.R.layout.simple_list_item_2 as a row item in your ListView. You need to create Custom Adapter so as to show both the text in the ListView. Set both text in getView() method. Also, use ViewHolder pattern for smooth scrolling on ListView.

protected void onPostExecute(String result) {
        JSONObject json = null;
        try {
            json = new JSONObject(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ArrayList<FollowingKeywords> keyWord = new ArrayList<FollowingKeywords>();
        JSONArray keywordArray = json.optJSONArray("keywords");
        if(keywordArray != null) {
            for (int i = 0; i < keywordArray.length(); i++) {
                JSONObject keywordObject = keywordArray.optJSONObject(i);
                if(keywordObject != null) {
                     FollowingKeywords keywords = new FollowingKeywords();
                     keywords.setKeyword(keywordObject.optString("keyword"));
                     keywords.setTimestamp(keywordObject.optString("creation_date"));
                     keyWord.add(keywords);
                }
            }
        }

        MyAdapter myAdapter = new MyAdapter(this, android.R.layout.simple_list_item_2, keyWord);
        ListView listView1 = (ListView) findViewById(R.id.listView1);
        listView1.setAdapter(myAdapter);
    }

MyAdapter.java

public class MyAdapter extends ArrayAdapter<FollowingKeywords>{

    Context context;
    List<FollowingKeywords> keywordList;

    public MyAdapter(Context context, int resource, ArrayList<FollowingKeywords> keywordList) {
        super(context, resource, keywordList);
        this.context = context;
        this.keywordList = keywordList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if(row == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = (View)inflater.inflate(android.R.layout.simple_list_item_2, null);

            holder = new ViewHolder();
            holder.keywordText = (TextView)row.findViewById(android.R.id.text1);
            holder.creationText = (TextView)row.findViewById(android.R.id.text2);

            row.setTag(holder);
        } else{
            holder = (ViewHolder) row.getTag();
        }

        holder.keywordText.setText(keywordList.get(position).getKeyword());
        holder.creationText.setText(keywordList.get(position).getTimestamp());

        return row;
    }

    static class ViewHolder {
        TextView keywordText;
        TextView creationText;
    }

}

这篇关于如何循环JSON数组和列表视图打印值? ANDROID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:34