ExpandableListView中的ExpandableListView获取正确的ID

时间:2014-12-11 10:44:03

标签: android json native expandablelistview

我有应用程序从资产中读取JSON到3个数组。接下来,这些数组是我在ExpandableListView中的父项和子项。第一个ExpandableListView中的Parent是TextView,而Child是第二个ExpandableListView。我为每个列表视图创建了两个适配器,但是当我点击每个主要父级时,会显示错误的子项。

public class MainParentAdapter extends BaseExpandableListAdapter {
    Context context;
    String[] MainparentList;
    String[] MainchildList;
    String[] child2;
    AssetManager assets;
    SecondAdapter sa;
    CustomList cust;
    public MainParentAdapter(Context context, AssetManager assets) throws IOException, JSONException {
        this.context=context;
        this.assets=assets;
        ParseJSON();
        cust=new CustomList(context);
        cust.setAdapter(sa);
    }

    @Override
    public int getGroupCount() {
        return MainparentList.length;
    }

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

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

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

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView tv=new TextView(context);
        tv.setText(MainparentList[groupPosition]+"  "+MainchildList[groupPosition]+"   "+child2[groupPosition]);
        tv.setTextSize(30);
        return tv;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        cust=new CustomList(context);
        cust.setAdapter(sa);
        cust.setGroupIndicator(null);
        return cust;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    public String loadJSON() {
        String json = null;
        try {
            InputStream is = assets.open("test.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");

        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
    public void ParseJSON() throws JSONException {
        JSONArray JA=new JSONArray(loadJSON());
        JSONObject jo=null;
        MainparentList=new String[JA.length()];
        MainchildList=new String[JA.length()];
        child2=new String[JA.length()];
        sa=new SecondAdapter(context);
        for (int i=0;i<MainparentList.length;i++) {
            jo = JA.getJSONObject(i);
            MainparentList[i] = jo.getString("name");
            MainchildList[i] = jo.getString("species");
            child2[i] = jo.getString("id");
        }
        sa.SecondParent=MainchildList;
        sa.SecondChild=child2;
    }
}

在这个主要父级中,我通过自定义ExpandableListView创建每个子级。在ParseJSON函数中,我获取JSON中对象的所有值,并将数组复制到第二个适配器,以在那里读取它们。

还有第二个适配器:

public class SecondAdapter extends BaseExpandableListAdapter
{
    Context context;
    String[] SecondParent;
    String[]SecondChild;
    boolean dip=false,dip2=false;

    public SecondAdapter(Context context) {
        this.context=context;

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

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return groupPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
    {
            TextView tv = new TextView(context);
            tv.setText(SecondChild[groupPosition]);
            tv.setPadding(150, 5, 5, 5);
            tv.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
            return tv;
    }

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

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

    @Override
    public int getGroupCount()
    {
        return 1;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
    {
        TextView tv = new TextView(context);
        tv.setText(SecondParent[groupPosition]);
        tv.setPadding(50, 7, 7, 7);
        return tv;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

Whren它显示,只显示第一个ExpandableListView。父母的每个孩子都具有与第一个孩子相同的值。我希望为父提供适当的值,因为它是用JSON文件编写的:

 [
  {
    "id": 0,
    "species": "Capra hircus",
    "name": "Goat"
  },
  {
    "id": 1,
    "species": "Panthera pardus",
    "name": "Leopard"
  },
  {
    "id": 2,
    "species": "Equus zebra",
    "name": "Zebra"
  },
  {
    "id": 3,
    "species": "Equus swinia",
    "name": "Swinia"
  }
]

对于每个名称应该显示物种,然后物种的孩子是id。但是所有元素(名称)都有相同的子元素。

enter image description here

那么我怎样才能在第二个名单中为父母找到合适的孩子?

感谢。

0 个答案:

没有答案