0

我需要执行以下操作:

  1. 从 url 加载 json 数据
  2. 根据json信息创建expandablelistview

我知道 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");
        }
    }
}

可扩展的.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;
    }
}
4

1 回答 1

0

在您拥有的地方:

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

创建 2 个数组,您将在适配器中使用它们来代替 createGroupList() 和 createChildList()。

于 2013-05-24T19:07:54.533 回答