hashmap在android中的expandable listview中显示数据

时间:2016-05-17 11:01:24

标签: java android listview hashmap expandablelistview

我试图在android中的服务器上显示可扩展列表视图中的数据。我正在尝试在AsyncTask中实现hashmap。请参阅以下代码。

private class HttpAsyncTaskReports extends AsyncTask<String, Void, String> {
    private ProgressDialog dialog;
    public HttpAsyncTaskReports(ExpandableActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected String doInBackground(String... urls) {
        return GET(urls[0]);
    }

    @Override
    protected void onPreExecute() {
        // Removes all markers, overlays, and polylines from the map.
        dialog.setMessage("Loading reports, please wait.");
        dialog.show();
    }

    @Override
    protected void onPostExecute(String result) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Header, Child data

        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
        top250.add("The Godfather");

        try {
            JSONArray jArray = new JSONArray(result);

            JSONObject jsonobject = jArray.getJSONObject(0);
            JSONArray jsonAssets = jsonobject.getJSONArray("last_data");
            Log.d("LASTLATLONG", result);

            for (i = 0; i < jsonAssets.length(); i++) {
                JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
                JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
                JSONObject lastData_timeJsonObject = lastData_JsonObject.getJSONObject("time");
//              JSONObject lastData_dateJsonObject = lastData_timeJsonObject.getJSONObject("$date");
//              JSONObject lastData_speedJsonObject = lastData_JsonObject.getJSONObject("speed");

                // Adding child data
                listDataHeader.add(lastData_timeJsonObject.getString("$date"));
                listDataChild.put(listDataHeader.get(i), top250);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Server Error :( Please wait!", Toast.LENGTH_LONG).show();
        }
    }
}

这是ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

这是错误......

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6856)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at gps.finder.com.findermaterial.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:75)
    at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
    at android.widget.ListView.setAdapter(ListView.java:502)
    at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:604)
    at gps.finder.com.findermaterial.ExpandableActivity.onCreate(ExpandableActivity.java:102)
    at android.app.Activity.performCreate(Activity.java:6550)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)

有人能告诉我该如何解决这个问题?

如果我使用类似下面的函数列出数据,它会成功运行。

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Top 50");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}

但我需要使用从服务器检索的数据来实现hashmap。

感谢您的建议。提前谢谢。

0 个答案:

没有答案