【问题标题】:JSON Parse to List ViewJSON 解析到列表视图
【发布时间】:2012-09-27 04:18:24
【问题描述】:

我正在为 link 解析 json,并且我想通过仅使用 ArrayLists 和 Array Adapter 在列表视图中显示解析后的数据。

但我得到的只是索引的最后一个值。

这是我的代码

JSONPARSE.java

public class JsonParse extends Activity {

    ArrayList<contactsGS> contacts = new ArrayList<contactsGS>();

    ListView lv_contacts;
    InputStream is = null;
    String jsonString;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_parse);

        lv_contacts = (ListView)findViewById(R.id.lv_jsonParse);

        is = CommonUtilJson.getInputStream("http://api.androidhive.info/contacts/");
        jsonString = CommonUtilJson.inputStreamToString(is);
        ParseCode parse = new ParseCode(jsonString);
        contacts=parse.parseValues();

        ArrayAdapter adapter = new ArrayAdapter<contactsGS>(this, android.R.layout.simple_list_item_1,contacts);
        lv_contacts.setAdapter(adapter);

    }

    public String getJsonSring(String api_url) throws URISyntaxException,
    ClientProtocolException, IOException {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            URL url = new URL(api_url);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
                    url.getQuery(), null);
            request.setURI(uri);

            HttpResponse response = client.execute(request);
            InputStream ips = response.getEntity().getContent();
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips));

            StringBuilder sb = new StringBuilder();
            String s;
            while (true) {
                s = buf.readLine();
                if (s == null || s.length() == 0)
                    break;
                sb.append(s);

            }
            buf.close();
            ips.close();
            return sb.toString();

        }

        finally {
            // any cleanup code...
        }

    }   
    }

这是我解析代码的类。

PARSECODE.java

    public class ParseCode {

        String jsonString;
        public static String url = "http://api.androidhive.info/contacts/";
        ArrayList<contactsGS> contactsData = new ArrayList<contactsGS>();
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String,String>>();
        contactsGS gtrSeter = new contactsGS();
        public ParseCode(String jsonString){
            this.jsonString = jsonString;
        }

        JSONObject jsonObj = null;

        public ArrayList<contactsGS> parseValues(){

            try{

                jsonObj = new JSONObject(jsonString);

                JSONArray jsonArray = jsonObj.getJSONArray("contacts");
                for(int index =0 ; index<jsonArray.length();index++){

                    JSONObject contacts = jsonArray.getJSONObject(index);

                    String id = contacts.getString("id");
                    gtrSeter.setId(id);
                    String name = contacts.getString("name");
                    gtrSeter.setName(name);
                    String email = contacts.getString("email");
                    gtrSeter.setEmail(email);
                    String address = contacts.getString("address");
                    gtrSeter.setAddress(address);
                    String gender = contacts.getString("gender");

                    JSONObject phoneObj = contacts.getJSONObject("phone");
                    String mobile = phoneObj.getString("mobile");
                    gtrSeter.setMobile(mobile);
                    String home = phoneObj.getString("home");
                    String office = phoneObj.getString("office");


                   contactsData.add(gtrSeter);

                }
            }
            catch(Exception e){

                e.printStackTrace();
            }
            return contactsData;
        }

    }

这是我的 Getter Setter 类

ContactGS.java

public class contactsGS {

    public String id;
    public String name;
    public String email;
    public String address;
    public String mobile;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    private static contactsGS singletonObject;

    public static contactsGS getSingletonObject(){

        if(singletonObject==null){
            singletonObject = new contactsGS();
        }
        return singletonObject;
    }

}

请帮助我。我想在列表视图的一行中获取一个人的详细信息。

提前致谢

【问题讨论】:

    标签: android json android-listview


    【解决方案1】:

    在循环中将每个contactGS添加到arraylist中,在for循环中创建ContactGS

    contactsGS gtrSeter = new contactsGS();
    

    在for循环的末尾添加这一行

    contactsData.add(gtrSeter );
    

    喜欢这个

    for(int index =0 ; index<jsonArray.length();index++){
    
       contactsGS gtrSeter = new contactsGS();
       JSONObject contacts = jsonArray.getJSONObject(index);
    
       String id = contacts.getString("id");
       gtrSeter.setId(id);
       String name = contacts.getString("name");
       gtrSeter.setName(name);
       String email = contacts.getString("email");
       gtrSeter.setEmail(email);
       String address = contacts.getString("address");
       gtrSeter.setAddress(address);
       String gender = contacts.getString("gender");
    
       JSONObject phoneObj = contacts.getJSONObject("phone");
       String mobile = phoneObj.getString("mobile");
       gtrSeter.setMobile(mobile);
       String home = phoneObj.getString("home");
       String office = phoneObj.getString("office");
    
       contactsData.add(gtrSeter);
    
    }
    

    【讨论】:

    • RajaReddy 是对的,你只是在重复使用同一个实例,你需要创建一个新实例并在每次迭代时将其放入列表中
    • Ok Raja,它帮助了我,但我现在只获取列表视图示例 contacsGS@44f6e150... 中的引用,等等,如何获取值,我在哪里做错了?
    • Raja,我也做了同样的事情,但我没有得到值。我只是在列表视图中获取这些东西-contactsGS@4567238,contactsGS@756345g,...等等??
    • 这个实例将改变进程contactsGS gtrSeter = new contactsGS();,并告诉我你的适配器类
    • 我已经在代码中展示了上面的适配器类。请看看拉贾。它在 JSONPARSE 类中
    【解决方案2】:

    请将您的代码更改如下。

    在公共类 ParseCode 中

    你定义的,在这里声明,每次循环初始化。

    contactsGS gtrSeter = new contactsGS();
    

    改变

    contactsGS gtrSeter;
    

    在公共 ArrayList parseValues() 方法中 在for循环中写第一条语句。

    gtrSeter = new contactsGS();
    

    所以你的代码应该是这样的。

    for(int index =0 ; index<jsonArray.length();index++){
       gtrSeter = new contactsGS();
       JSONObject contacts = jsonArray.getJSONObject(index);
    .
    .
    .
    .
    .
    .
    .
    }
    

    【讨论】:

    • @GauravArora 如果两个人同时打字会发生什么?,我在发布后才知道上面的答案,因此我没有复制粘贴。
    • 好的 jignesh 很抱歉弄得一团糟。谢谢。你能帮我进一步吗?
    • 请阅读我上面与 RAJA REDDY 的 cmets 并帮助我兄弟.. 请
    • 是的,根据 RAJA REDDY 请编辑您的问题并添加您的 ArrayAdapter 类
    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多