Android Studio,如何从JSON凌空生成ExpandableListView?

时间:2017-04-03 04:37:43

标签: android json listview expandablelistview

谁能帮助我吗? 我尝试使用POST方法生成expandablelistview来自服务器的数据。但是我得到了错误: ' int java.util.List.size()'在null对象引用上 这是我的代码



private ExpandableListView listView;
    private ExpandableListAdapter listAdapter;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listHash;
    Button btnSearch;
    EditText search;
    String kata;
    String statutory_url = "http://ds.bki.co.id:7777/ds/android/datasurvey.php";

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

        btnSearch = (Button) findViewById(R.id.btnSearch);
        search = (EditText) findViewById(R.id.editText);
        listView = (ExpandableListView) findViewById(R.id.ExpList);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                kata = search.getText().toString();
                getData();
            }
        });
        listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
        listView.setAdapter(listAdapter);
    }

    private void getData(){
        RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, statutory_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONArray jsonArray = new JSONArray(response);
                    listDataHeader = new ArrayList<>();
                    listHash = new HashMap<>();
                    final int result = jsonArray.length();
                    for (int i = 0; i < result; i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String surv = jsonObject.getString("survey");
                        String last = jsonObject.getString("last");
                        String next1 = jsonObject.getString("next1");
                        String next2 = jsonObject.getString("next2");
                        String surveyor = jsonObject.getString("surveyor");

                        listDataHeader.add(surv);
                        List<String> data = new ArrayList<>();
                        data.add(last);
                        data.add(next1);
                        data.add(next2);
                        data.add(surveyor);

                        listHash.put(listDataHeader.get(i), data);

                    }


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

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        })
        {
            @Override
            protected Map<String, String> getParams()throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();

                params.put("noregister", kata);
                return params;
            }
        };
        queue.add(stringRequest);
    }
}
&#13;
&#13;
&#13;

这是我的适配器

&#13;
&#13;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHashMap;

    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return listHashMap.get(listDataHeader.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return listDataHeader.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return listHashMap.get(listDataHeader.get(i)).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String headerTitle = (String)getGroup(i);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_group, null);
        }
        TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final String childText = (String)getChild(i,i1);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item, null);
        }
        TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我没有查看完整的代码。我发现错误onCreate自我启动。

   listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
   listView.setAdapter(listAdapter);

此处listDataHeaderlistHash都为空。您删除此行格式onCreate并将此行放在getData方法中。

 private void getData(){
    RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.POST, statutory_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONArray jsonArray = new JSONArray(response);
                listDataHeader = new ArrayList<>();
                listHash = new HashMap<>();
                final int result = jsonArray.length();
                for (int i = 0; i < result; i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String surv = jsonObject.getString("survey");
                    String last = jsonObject.getString("last");
                    String next1 = jsonObject.getString("next1");
                    String next2 = jsonObject.getString("next2");
                    String surveyor = jsonObject.getString("surveyor");

                    listDataHeader.add(surv);
                    List<String> data = new ArrayList<>();
                    data.add(last);
                    data.add(next1);
                    data.add(next2);
                    data.add(surveyor);

                    listHash.put(listDataHeader.get(i), data);
                    // Adapter code is added here.  
                    listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
                    listView.setAdapter(listAdapter);
                }


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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    })
    {
        @Override
        protected Map<String, String> getParams()throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();

            params.put("noregister", kata);
            return params;
        }
    };
    queue.add(stringRequest);
}

尝试从onCreate删除列表适配器并将其放在getData中。再次检查此代码并重新运行此方法。