如何在可扩展列表视图中显示项目

时间:2018-09-19 13:06:19

标签: java android listview

在可扩展列表视图中,我想将target_name的列表显示为子项,将域显示为标题,但是活动中什么都没有显示,只是空白的白色活动,请提出建议。

这是我的主要活动:

公共类MainActivity扩展了AppCompatActivity {

ExpandableListAdapter ExpAdapter;
ExpandableListView ExpandList;
List<String> listDataHeader;
HashMap<String, List<String>> listHash;

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

    ExpandList = (ExpandableListView) findViewById(R.id.expandableListView);

    ExpandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    " List Expanded.", Toast.LENGTH_SHORT).show();


        }
    });

    ExpandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {

            Toast.makeText(getApplicationContext(),
                    " List Collapsed.", Toast.LENGTH_SHORT).show();

        }
    });

    ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(getApplicationContext(),
                    "child", Toast.LENGTH_SHORT).show();

            return false;
        }
    });

    makejsonobjreq();
}

private void makejsonobjreq() {

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://54.146.132.94/webservices/AllocatedtargetBydomain?user_id=45",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        listDataHeader = new ArrayList<>();
                        listHash = new HashMap<>();


                        for (int i = 0; i < jsonArray.length(); i++){

                            JSONObject jsonObject= jsonArray.getJSONObject(i);
                            String domain_name = jsonObject.getString("domain");
                            listDataHeader.add(domain_name);

                            List<String> data = new ArrayList<>();
                            JSONArray jsonArray1= jsonObject.getJSONArray("target_set");
                            for(int j=0; i<jsonArray1.length(); j++){

                                JSONObject jsonObject1= jsonArray1.getJSONObject(j);
                                String target_name = jsonObject1.getString("target_name");
                                /*String target_id = jsonObject1.getString("target_id");*/

                                data.add(target_name);
                                /*data.add(target_id);*/
                            }

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

                            // Adapter code is added here.
                            ExpAdapter = new ExpandListAdapter(MainActivity.this,listDataHeader,listHash);
                            ExpandList.setAdapter(ExpAdapter);
                        }

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

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, "" + error.toString(), Toast.LENGTH_SHORT).show();
        }
    });
    requestQueue.add(stringRequest);

}

}

这是ExpandListAdapter:

class ExpandListAdapter扩展了BaseExpandableListAdapter {

Context context;
List<String> listDataHeader;
HashMap<String,List<String>> listHashMap;

public ExpandListAdapter(Context mainActivity, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {

    this.context = mainActivity;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
}


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

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

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

@Override
public Object getChild(int groupPosition, int childPosition) {

    return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition,
                         boolean isExpanded, View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);

    }
    TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);

    return convertView;
}

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

}

0 个答案:

没有答案