Разобрать данные JSON в ExpandableListView, используя Volley - PullRequest
0 голосов
/ 24 марта 2019

Я хочу проанализировать мои данные JSON в expandablelistview, в то время как при щелчке по родительскому представлению он должен возвращать свой определенный список дочерних представлений.Но я не вижу ни родительского взгляда, ни ребенка.Как я пытался отладки, он не возвращает значение в массиве (т.е. subcatarray в моем состоянии) второго цикла For.По моим данным, при нажатии « theatre » должно возвращаться « Luke », а при нажатии « mall » должно возвращаться » Advik и Бипаша"Пожалуйста, помогите мне получить данные в расширяемом списке просмотра.

Данные JSON:

{
"stds": [
    {
        "stopNum": "0",
        "stopName": "one",
        "loc": "23.097278,72.446139"
    },
    {
        "stopNum": "1",
        "stopName": "theatre",
        "loc": "23.073028,72.5245",
        "students": [
            {
                "studentName": "Luke",
                "studentId": "58eb423bcf51a2bd759a740a"
            }
        ]
    },
    {
        "stopNum": "2",
        "stopName": "flyover",
        "loc": "23.07,72.521917",
        "students": [
            {
                "studentName": "Leia",
                "studentId": "58eb423ccf51a2bd759a740b"
            }
        ]
    },
    {
        "stopNum": "3",
        "stopName": "mall",
        "loc": "23.076222,72.50775",
        "students": [
            {
                "studentName": "Advik",
                "studentId": "58eb423ccf51a2bd759a740d"
            },
            {
                "studentName": "Bipasha",
                "studentId": "58eb423ccf51a2bd759a740e"
            }
        ]
    },
    {
        "stopNum": "4",
        "stopName": "alpha",
        "loc": "23.093639,72.495972",
        "students": [
            {
                "studentName": "Aarav",
                "studentId": "58eb423ccf51a2bd759a740c"
            }
        ]
    },
    {
        "stopNum": "5",
        "stopName": "reliance",
        "loc": "23.097278,72.446139",
        "students": []
    }
  ]
}

Родительское Pojo:

public class ParentPojo {

private String mCreator;
private String mid;

private ArrayList<ChildPojo> ch_list;

public ParentPojo(String mid, String creator, ArrayList<ChildPojo> ch_list) {

    this.mid = mid;
    this.mCreator = creator;
    this.ch_list = ch_list;
}

public String getmCreator() {
    return mCreator;
}

public void setmCreator(String mCreator) {
    this.mCreator = mCreator;
}

public String getMid() {
    return mid;
}

public void setMid(String mid) {
    this.mid = mid;
}

public String getCreator() {
    return mCreator;
}

public String getId() {
    return mid;
}

public ArrayList<ChildPojo> getItems(){

    return ch_list;
   }
}

Ребенок Pojo:

public class ChildPojo {

private String id;
private String name;

public ChildPojo(String id, String name) {

    this.id = id;
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
   }
}

Класс адаптера:

 public class MyExListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<ParentPojo> parentPojos;

public MyExListAdapter(Context context, ArrayList<ParentPojo> parentlist){

    this.context = context;
    this.parentPojos = parentlist;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {

    ArrayList<ChildPojo> chList = parentPojos.get(listPosition).getItems();
    return chList.get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    ChildPojo childpojo = (ChildPojo) getChild(listPosition, expandedListPosition);

    if (convertView == null) {
        LayoutInflater layoutinflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflator.inflate(R.layout.list_child, null);

    }

    TextView textView = convertView.findViewById(R.id.textChild);
    textView.setText(childpojo.getName());

    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {

    ArrayList<ChildPojo> chlist = parentPojos.get(listPosition).getItems();
    return chlist.size();
}

@Override
public ParentPojo getGroup(int listPosition) {
    return parentPojos.get(listPosition);
}

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

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

@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    ParentPojo parentPojo = getGroup(listPosition);

    if (convertView == null) {
        LayoutInflater layoutinflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflator.inflate(R.layout.list_parent, null);

    }

    TextView textView = convertView.findViewById(R.id.textParent);
    textView.setText(parentPojo.getCreator());

    return convertView;
}

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return true;
   }

}

MainActivity:

private RequestQueue mRequestQueue;
JSONArray subcatarray, jarray;
JSONObject obj, subObj;

private ArrayList<ParentPojo> parentlist;
private ArrayList<ChildPojo> childlist;

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

    mRequestQueue = Volley.newRequestQueue(this);
    jsonParse();

}

 public void jsonParse() {

    final String url = ("https://api.myjson.com/bins/7pxzm");

    StringRequest request = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    Log.i("response", url + "response:" + response);

                    try {
                        obj = new JSONObject(response);
                        jarray = obj.getJSONArray("stds");

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

                                JSONObject jsonObject = jarray.getJSONObject(i);

                                String item_id = obj.getString("stopNum");
                                String creatorName = obj.getString("stopName");

                                Log.i("response", creatorName + "");

                                subcatarray = jsonObject.getJSONArray("students");

                                for (int j = 0; j < subcatarray.length(); j++) {

                                    childlist = new ArrayList<>();

                                    subObj = subcatarray.getJSONObject(j);

                                    String studentId = obj.getString("studentId");
                                    String studentName = obj.getString("studentName");

                                    Log.i("response", studentName + "");

                                    childlist.add(new ChildPojo(studentId, studentName));
                                }

                                parentlist = new ArrayList<ParentPojo>();
                                parentlist.add(new ParentPojo(item_id, creatorName, childlist));

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

                                MyExListAdapter adapter = new MyExListAdapter(MainActivity.this, parentlist);
                                list.setAdapter(adapter);
                        }

                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "Error Parsing Data", Toast.LENGTH_LONG);
                        e.printStackTrace();
                    }catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG);
                    }
                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            Toast.makeText(MainActivity.this, "No items available", Toast.LENGTH_SHORT).show();
        }
    });

    mRequestQueue.add(request);
}
...