加载JSON数据并创建ExpandableListView

时间:2013-05-24 18:58:46

标签: java android json http expandablelistview

我需要做以下事情:

  1. 从网址
  2. 加载json数据
  3. 根据json信息创建一个expandablelistview
  4. 我知道http请求必须是异步的,所以我使用AsyncTask来完成这项工作,但在我获取数据后,我无法创建可扩展的。

    虽然我正确地获取了json数据,但是现在我正在使用静态信息来构建可扩展的数据。

    请帮助我,我昨天开始编写Android代码,我想确定我在做什么。

    提前致谢。

    MainActivity.java:

    package com.hp.package1;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            new getJson().execute();
        }
    }
    

    getJson.java:

    package com.hp.package1;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import android.os.AsyncTask;
    
    public class getJson extends AsyncTask<String, Float, Integer> {
        public Integer doInBackground(String[] url) {
            JSONArray names;
            JSONArray values;
    
            String uri = "http://www.domain.com/json.php";
    
            try {
                HttpClient   httpclient = new DefaultHttpClient();
                HttpGet      httpget    = new HttpGet(uri);
                HttpResponse response   = httpclient.execute(httpget);
                HttpEntity   entity     = response.getEntity();
                InputStream  instream   = entity.getContent();
                String       result     = convertStreamToString(instream);
                JSONObject   json       = new JSONObject(result);
    
                names  = json.names();
                values = json.toJSONArray(names);
    
                for (int m = 0; m < values.length(); m++)
                    System.out.println(names.getString(m) + " - " + values.getString(m));
    
                return 1;
            } catch (Exception e) {
                System.out.println("error: " + e.toString());
    
                return 0;
            }
        }
    
        private String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb      = new StringBuilder();
            String line           = null;
    
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
    
            }
    
            return sb.toString();
        }
    
        protected void onPostExecute(Integer bytes) {
            System.out.println("now comes the creation of expandable");
    
            try {
                new expandable(); // here is where I try to create the expandable
            } catch (Exception e) {
                System.out.println("error while creating the expandable");
            }
        }
    }
    

    expandable.java:

    package com.hp.package1;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import android.app.ExpandableListActivity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ExpandableListView;
    import android.widget.SimpleExpandableListAdapter;
    
    public class expandable extends ExpandableListActivity {
        @SuppressWarnings("unchecked")
        public expandable() {
            System.out.println("constructor");
    
            SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
                        this,
                        createGroupList(),
                        R.layout.group_row,
                        new String[] { "Group Item" },
                        new int[] { R.id.row_name },
                        createChildList(),          
                        R.layout.child_row,         
                        new String[] {"Sub Item"},  
                        new int[] { R.id.grp_child} 
            );
    
            setListAdapter(expListAdapter);
        }
    
        @SuppressWarnings("unchecked")
        private List createGroupList() {
            ArrayList result = new ArrayList();
    
            for (int i = 0; i < 15; ++i) {
                HashMap m = new HashMap();
                m.put( "Group Item","Group Item " + i );
                result.add( m );
            }
    
            return (List)result;
        }
    
        @SuppressWarnings("unchecked")
        private List createChildList() {
            ArrayList result = new ArrayList();
    
            for (int i = 0; i < 15; ++i ) {
                ArrayList secList = new ArrayList();
    
                for (int n = 0; n < 3; n++ ) {
                    HashMap child = new HashMap();
                    child.put( "Sub Item", "Sub Item " + n );
                    secList.add(child);
                }
    
                result.add(secList);
            }
    
            return result;
        }
    
        public void onContentChanged () {
            System.out.println("onContentChanged");
            super.onContentChanged();
        }
    
        public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
            return true;
        }
    
        public void onGroupExpand (int groupPosition) {
            try {
    
            } catch(Exception e) {
    
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

在你所在的地方:

 for (int m = 0; m < values.length(); m++)
                System.out.println(names.getString(m) + " - " + values.getString(m));

在适配器中创建将用于代替createGroupList()和createChildList()的2个数组。