【问题标题】:Android Studio, How to generate ExpandableListView from JSON volley?Android Studio,如何从 JSON 凌空生成 ExpandableListView?
【发布时间】:2017-04-03 04:37:43
【问题描述】:

谁能帮助我? 我尝试使用 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;
    }
}

【问题讨论】:

    标签: android json listview expandablelistview


    【解决方案1】:

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

    【讨论】:

    • 这是我的荣幸。一旦您对这个答案感到满意,请点赞。 :)
    • 感谢@AMANSINGH 这也帮助了我
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2016-12-27
    • 2023-03-29
    相关资源
    最近更新 更多