嗨,我想从json创建三级可扩展列表视图。
我已经创建了可扩展的列表视图,处于两个两个级别,但是三个级别的我都没有怎么做,如果有人请请帮助我完成它。
这是我创建的代码。

我的主要活动

String Tag="MainActivity";
private ExpandableListView expandableListView;
ArrayList<String>mainlist =new ArrayList<>();;
HashMap<String, List<String>>  childContent = new HashMap<String, List<String>>();

HashMap<String, HashMap<List<String>,List<String>>>  subchildContent = new  HashMap<String, HashMap<List<String>,List<String>>>();
List<String> childlist;
List<String> subchildlist;
private List<String> parentHeaderInformation;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new ReleaseOverviewReleaseAsynchTask().execute();
}

@Override
protected void onResume() {
    super.onResume();

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

    //ExpandableListAdapter expandableListViewAdapter = new ExpandableListAdapter(getApplicationContext(), mainlist, childContent);
    ChildSubChildExpandableListAdapter adapter=new ChildSubChildExpandableListAdapter(getApplicationContext(),mainlist,childlist,childContent);
    expandableListView.setAdapter(adapter);
}



class ReleaseOverviewReleaseAsynchTask extends AsyncTask<Void,Void,Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        String jsonStr = Util.loadJSONFromAsset(MainActivity.this,"release.json");
        if (jsonStr != null) {
            try {
                JSONArray array = new JSONArray(jsonStr);

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

                    JSONObject jo_object = array.getJSONObject(i);
                    childlist= new ArrayList<String>();
                    subchildlist= new ArrayList<String>();
                    String release= jo_object.get("Release").toString();
                    mainlist.add(jo_object.get("Release").toString());
                    Log.i("main release",release);
                    JSONArray jsonArrayDetails=jo_object.getJSONArray("details");
                    if (!jsonArrayDetails.isNull(i)){
                        JSONObject jo_object1 = jsonArrayDetails.getJSONObject(i);

                        String builddate=  jo_object1.get("Build Date").toString();
                        Log.i(Tag,builddate);
                        String deploydate= jo_object1.get("Deploy Date").toString();
                        Log.i(Tag,deploydate);
                        String respindate= jo_object1.get("Respin Date").toString();
                        Log.i(Tag,respindate);
                        String releaselead= jo_object1.get("Release Lead").toString();
                        Log.i(Tag,releaselead);
                        String engineerlead= jo_object1.get("Engineering Lead").toString();
                        Log.i(Tag,engineerlead);
                        String testlead=jo_object1.get("Test Lead").toString();
                        Log.i(Tag,testlead);


                    }
                    JSONArray childarray=jo_object.getJSONArray("children");

                    for (int i_child = 0; i_child < childarray.length(); i_child++) {
                        JSONObject jo_objectchild = childarray.getJSONObject(i_child);

                        childlist.add(jo_objectchild.get("Release").toString());
                        Log.i("child release",jo_objectchild.get("Release").toString());

                        JSONArray subchildarray=jo_objectchild.getJSONArray("children");

                        for (int i_subchild=0;i_subchild<=subchildarray.length();i_subchild++){
                            if (!subchildarray.isNull(i_subchild)){
                                JSONObject jo_objectsubchild = subchildarray.getJSONObject(i_subchild);
                                Log.i("sub release",jo_objectsubchild.get("Release").toString());
                                subchildlist.add(jo_objectsubchild.get("Release").toString());
                            }

                            /*JSONObject jo_objectsubchild = subchildarray.getJSONObject(i_subchild);
                           */
                        }



                    }
                    childContent.put(mainlist.get(i),subchildlist);
                }


            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        else{
            //  Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }
        return null;
    }


我的父级可扩展列表视图是

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> parentDataSource;
private HashMap<String, List<String>> childDataSource;

private HashMap<String, List<String>> subchildDataSource;

public ExpandableListAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child){

    this.context = context;

    this.parentDataSource = childParent;

    this.childDataSource = child;
}
@Override
public int getGroupCount() {
    return this.parentDataSource.size();
}

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

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

}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.childDataSource.get(parentDataSource.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) {
    View view = convertView;
    if(view == null){

        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.parentlayout, parent, false);

    }
    String parentHeader = (String)getGroup(groupPosition);

    TextView parentItem = (TextView)view.findViewById(R.id.parent_layout);

    parentItem.setText(parentHeader);
    notifyDataSetChanged();
    return view;
}

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

    if(view == null){

        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.childlayout, parent, false);

    }
    String childName = (String)getChild(groupPosition, childPosition);

    TextView childItem = (TextView)view.findViewById(R.id.child_layout);

    childItem.setText(childName);
    notifyDataSetChanged();
    return view;
}

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


这是三个级别的可扩展列表,不适用于我

public class ChildSubChildExpandableListAdapter extends
BaseExpandableListAdapter{

Context context;
HashMap<String, List<String>> childdata;

private List<String> parentDataSource;
List<String> secondLevel;
private HashMap<String, List<String>> childDataSource;

public ChildSubChildExpandableListAdapter(Context context, List<String> Parent, List<String> secondLevel, HashMap<String, List<String>> data) {
    this.context = context;

    this.parentDataSource = Parent;

    this.secondLevel = secondLevel;

    this.childdata = data;


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

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

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

@Override
public Object getChild(int i, int i1) {
    return i1;
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
    View view = convertView;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.parentlayout, null);
    TextView text = (TextView) convertView.findViewById(R.id.parent_layout);
    text.setText(this.parentDataSource.get(groupPosition));
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup group) {
    final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);

    String headers =secondLevel.get(groupPosition);
    List<String> header=new ArrayList<>();
    header.add(headers);
    List<List<String>> childData = new ArrayList<>();
   HashMap<String, List<String>> secondLevelData= (HashMap<String, List<String>>) childdata.get(groupPosition);
    for(String key : secondLevelData.keySet())
    {


        childData.add(secondLevelData.get(key));

    }

    secondLevelELV.setAdapter(new ExpandableListAdapter(context, header,secondLevelData));

    secondLevelELV.setGroupIndicator(null);

    secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousGroup = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if(groupPosition != previousGroup)
                secondLevelELV.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });
    return secondLevelELV;
}

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


}

这是第二级可扩展列表视图

  public class SecondLevelExpandableListView extends ExpandableListView
  {

    public SecondLevelExpandableListView(Context context) {
        super(context);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


嗨,朋友们,请帮助我创建三个级别的可扩展性。如果我仅使用ExpandableListAdapter,则上面的代码有效。
在上面的代码中,我已经将childSubChildAdapter用于三个级别,所以它不起作用

我想在列表的每个元素中添加复选框,如果我选择一个孩子,则还应该选择其父对象

这是我的杰森

{
"Release": "16.1",
"details": [],
"children": [
  {
    "Release": "16.1R3",
    "details": [
      {
        "Build Date": "",
        "Deploy Date": "",
        "Respin Date": "",
        "Release Lead": "Jl",
        "Engineering Lead": "LG",
        "Test Lead": "Dl"
      }
    ],
    "children": [
      {
        "Release": "16.1R3-S5",
        "details": [
          {
            "Build Date": "2017-08-17",
            "Deploy Date": "2017-08-25",
            "Respin Date": "",
            "Release Lead": "Jk",
            "Engineering Lead": "Ly",
            "Test Lead": "FD"
          }
        ]
      }
    ]
  }


请帮助我解决这个问题。我是第一次做这类工作。

提前致谢

最佳答案

我的建议是使用回收站视图而不是可扩展列表视图,因为在大数据可扩展列表视图的情况下会产生问题,尤其是在三级可扩展列表视图的情况下。

并为列表的每个级别创建一个模型类,这些模型类具有用于复选框单击的布尔值。

关于android - 如何在Android中通过JSON创建三级可扩展 ListView ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45748611/

10-08 23:54