【问题标题】:Android add JSON to <string>Android 将 JSON 添加到 <string>
【发布时间】:2017-06-15 18:13:52
【问题描述】:

我想将json添加到listview,

但我不知道

我用这样的列表视图列出项目;

public class ListSectionActivity extends Activity {

    private AndroidListAdapter list_adapter;
    private ListView lv_android;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_section);

        DataCollection.data_arr=new ArrayList<DataCollection>();

        DataCollection.data_arr.add(new DataCollection("apple"));
        DataCollection.data_arr.add(new DataCollection("car"));


        lv_android=(ListView)findViewById(R.id.lv_android);

        list_adapter=new AndroidListAdapter(ListSectionActivity.this, R.layout.list_item,DataCollection.data_arr);


        ListSeparateAdapter<DataCollection> listsectionAdapter = new ListSeparateAdapter<DataCollection>(ListSectionActivity.this,
                list_adapter, R.layout.enroll_section_list, R.id.tv_section);

        lv_android.setAdapter(listsectionAdapter);

    }

}

但我想从 json 中列出项目。 我的 json 代码:

@Override
        protected void onPostExecute(String content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray jsonArray =  jsonObject.getJSONArray("main");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject c = jsonArray.getJSONObject(i);
                    JSONArray server = c.getJSONArray("home");
                    for(int j=0; j<server.length(); j++){
                        JSONObject serverObject = server.getJSONObject(j);

                                   string rank = serverObject.getString("rank");


                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

我的 json;

{
  "main":[
    {
      "home":[
        {"rank":"33"},
        {"rank":"72"},
        {"rank":"49"}
      ]
    }   
  ]
}

我想在我的列表视图中添加“字符串排名”。

我该怎么做?

谢谢。

编辑:我可以将 json 添加到 listview 但我只能列出第一个 json 项目。我的新代码

public class ListSectionActivity extends Activity {

    private AndroidListAdapter list_adapter;
    private ListView lv_android;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_section);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new ReadJSON().execute("url");
            }
        });
    }

    class ReadJSON extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            return readURL(params[0]);
        }

        @Override
        protected void onPostExecute(String content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray jsonArray =  jsonObject.getJSONArray("main");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject c = jsonArray.getJSONObject(i);
                    JSONArray server = c.getJSONArray("home");
                    for(int j=0; j<server.length(); j++){
                        JSONObject serverObject = server.getJSONObject(j);


                        String rank = serverObject.getString("rank");

                        DataCollection.data_arr=new ArrayList<DataCollection>();
                        DataCollection.data_arr.add(new DataCollection(rank));
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            lv_android=(ListView)findViewById(R.id.lv_android);

            list_adapter=new AndroidListAdapter(ListSectionActivity.this, R.layout.list_item,DataCollection.data_arr);

            ListSeparateAdapter<DataCollection> listsectionAdapter = new ListSeparateAdapter<DataCollection>(ListSectionActivity.this,
                    list_adapter, R.layout.enroll_section_list, R.id.tv_section);

            lv_android.setAdapter(listsectionAdapter);
        }
    }


    private static String readURL(String theUrl) {
        StringBuilder content = new StringBuilder();
        try {

            URL url = new URL(theUrl);

            URLConnection urlConnection = url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

}

数据集合

public class DataCollection {
    public String name;

    public static ArrayList<DataCollection> data_arr=null;
    public DataCollection(String name){


        this.name=name;
    }
}

【问题讨论】:

  • 在这里也发布 json
  • 我已经添加了 json @quicklearner
  • 使 DataCollection.data_arr 全局化,而不是在 oncreate 内部,因此如果在后执行时在同一类中或不同时,您可以直接添加 data_arr 将 data_arr 的引用发送到 asynctask 以便它可以从那里改变。
  • 嗨阿米特辛哈,我已经更新了我的帖子。请检查一下。谢谢@AmitSinha

标签: android json listview


【解决方案1】:

更新:您可以在 onPostexecute 中执行此操作。将排名数组保存在列表视图中并在适配器中使用它。

protected void onPostExecute(String content) {
  DataCollection.data_arr=new ArrayList<DataCollection>();
    try {

        JSONObject jsonObject = new JSONObject(content);
        JSONArray jsonArray =  jsonObject.getJSONArray("main");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject c = jsonArray.getJSONObject(i);
            JSONArray server = c.getJSONArray("home");
            for(int j=0; j<server.length(); j++){
                JSONObject serverObject = server.getJSONObject(j);

                String rank = serverObject.getString("rank");

                    DataCollection.data_arr.add(new DataCollection(rank));


            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(!data_arr.isEmpty()){

        //you can set the adapter and listview here. Something like this
        your_list_adapter=new AndroidListAdapter(this, R.layout.list_item,data_arr);

        your_lv_android.setAdapter(list_adapter);


    }
}

【讨论】:

  • 您好,我已经更新了我的帖子。请检查一下。谢谢
  • 好的。 DataCollection 类是什么样的?另外,您想在列表视图的每一行中显示什么?
  • 我在帖子中添加了 DataCollection。我想在列表视图中显示所有排名,但我只能显示第一名。
  • 我已经编辑了我的答案。您正在声明 DataCollection.data_arr=new ArrayList();在每次覆盖并创建新数组对象的 for 循环中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多