Android Studio,如何从 JSON volley 生成 ExpandableListView?

Android Studio, How to generate ExpandableListView from JSON volley?

谁能帮帮我? 我尝试使用 POST 方法在来自服务器的数据所在的位置创建可扩展列表视图。但我得到了错误: 'int java.util.List.size()' 空对象引用 这是我的代码

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);
    }
}

这是我的适配器

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;
    }
}

我没有查看你的完整代码。我发现自己启动 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。再次检查此代码并重新运行此方法。