How can I get the selected data from an expandable list, and then load the children?

2

I'm making an expandable list, but I do not know how to get the selected item, to check if it has children or not, and can load them from the server. Any ideas on how to implement it.

MainActivity

ArrayList<String> parent;
ArrayList<String> child;
LinkedHashMap<String, String[]> thirdLevelGames = new LinkedHashMap<>();

List<String[]> secondLevel = new ArrayList<>();

List<LinkedHashMap<String, String[]>> data = new ArrayList<>();

//String[] parent = new String[]{};

ThreeLevelListAdapter threeLevelListAdapterAdapter = null;

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

    progressDialog = new ProgressDialog(MainActivity.this);
    requestQueue = Volley.newRequestQueue(MainActivity.this);

    parent= new ArrayList<String>();

    Hijos();

    secondLevel.add(blusas);
    secondLevel.add(accesorio);


    thirdLevelBlusas.put(blusas[0], Escote);
    thirdLevelBlusas.put(blusas[1], Larga);
    thirdLevelBlusas.put(blusas[2], Corta);

    thirdLevelAccesorio.put(accesorio[0], Collar);
    thirdLevelAccesorio.put(accesorio[1], Reloj);
    thirdLevelAccesorio.put(accesorio[2], Anillo);
    thirdLevelAccesorio.put(accesorio[3], Pulsera);

    data.add(thirdLevelAccesorio);
    data.add(thirdLevelBlusas);

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

    threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);

    expandableListView.setAdapter(threeLevelListAdapterAdapter);


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

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


}

public void Hijos() {

    progressDialog.setMessage("Espere...");
    progressDialog.show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Categorias.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String ServerResponse) {

                    progressDialog.dismiss();

                    try {

                        JSONObject j = new JSONObject(ServerResponse);
                        String returnState = j.getString("returnState");
                        String returnData = j.getString("returnData");

                        JSONObject mainObject = new JSONObject(returnData);

                        Iterator<String> keys = mainObject.keys();
                        while (keys.hasNext()) {

                            String key = keys.next();
                            Log.i("Parser", "objeto : " + key);
                            JSONObject jsonObject1 = mainObject.getJSONObject(key);

                            String cate_name = jsonObject1.getString("cate_name");
                            String cate_id = jsonObject1.getString("cate_shop_id");

                            parent.add(cate_id);

                        }
                        Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG).show();

                        Toast.makeText(MainActivity.this, ""+ parent, Toast.LENGTH_LONG).show();

                        threeLevelListAdapterAdapter.notifyDataSetChanged();

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

                }
            }
            ,
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                    Toast.makeText(MainActivity.this, "Verifique su conexion", Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<String, String>();

            params.put("id",id);

            return params;
        }

    };

    // Creating RequestQueue.
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

    // Adding the StringRequest object into requestQueue.
    requestQueue.add(stringRequest);

}

}

Adapter

public class ThreeLevelListAdapter extends BaseExpandableListAdapter {

ArrayList<String> parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;

public ThreeLevelListAdapter(Context context, String[] parent, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
    this.context = context;

    this.secondLevel = secondLevel;

    this.data = data;
}

public ThreeLevelListAdapter(MainActivity context, ArrayList<String> parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
    this.context = context;

    this.parentHeaders=parentHeader;

    this.secondLevel = secondLevel;

    this.data = data;
}

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

}

@Override
public int getChildrenCount(int groupPosition) {


    // no idea why this code is working

    return 1;

}

@Override
public Object getGroup(int groupPosition) {

    return groupPosition;
}

@Override
public Object getChild(int group, int child) {


    return child;


}

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

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

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

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_first, null);
        TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
        text.setText(this.parentHeaders.get(groupPosition));

    return convertView;
}

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

    final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);

    String[] headers = secondLevel.get(groupPosition);


    List<String[]> childData = new ArrayList<>();
    HashMap<String, String[]> secondLevelData=data.get(groupPosition);

    for(String key : secondLevelData.keySet())
    {


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

    }



    secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers,childData));

    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 groupPosition, int childPosition) {
    return true;
}

}

    
asked by Maria Isabel 30.08.2018 в 15:51
source

1 answer

0

try placing an OnChildClick

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

      @Override
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
          ((TextView) v.findViewById(R.id.rowParentText)).getText();
          return true;
       }
});
    
answered by 30.08.2018 в 17:24