Dinesh Rijal
Dinesh Rijal

Reputation: 137

Androidparse json into expandable view

I want to implement something like this:

i want to implement  like this

This is the Parent View. When I click on this, I have to display its ExpandableListView: This is parent view when i click on this i have to display its expandable view

public class MainActivity extends AppCompatActivity {
    ProgressDialog PD;

    private ExpandListAdapter ExpAdapter;
    private ExpandableListView ExpandList;
    ArrayList<Group> list = new ArrayList<Group>();
    ArrayList<Child> ch_list= new ArrayList<Child>();``
    String url=ApplicationConstant.THIRD_WEB_CALL_HISTORY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_history);
        ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

        PD = new ProgressDialog(this);
        PD.setMessage("Loading.....");
        PD.setCancelable(false);
        new HistoryAsynktask().execute(url);

    }
    class  HistoryAsynktask extends AsyncTask<String ,String ,String> {

        @Override
        protected String doInBackground(String... uri) {
            HttpClient httpClient= new DefaultHttpClient();
            HttpResponse httpResponse;
            String histry_result = null;

            try {
                httpResponse=httpClient.execute(new com.loopj.android.http.HttpGet(uri[0]));
                StatusLine statusLine=httpResponse.getStatusLine();
                if (statusLine.getStatusCode()== HttpStatus.SC_OK){
                    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
                    httpResponse.getEntity().writeTo(byteArrayOutputStream);
                    histry_result=byteArrayOutputStream.toString();
                    JSONObject jsonObject= new JSONObject(histry_result);
                    JSONObject jsonObject1= jsonObject.getJSONObject("GetLastTxnsCustResult");
                    String status= jsonObject1.getString("status");
                    JSONArray jsonArray = jsonObject1.getJSONArray("txns");

                    for (int i =0 ;i< jsonArray.length();i++)
                    {
                        Child child = new Child();
                        Group group= new Group();
                        JSONObject jsonObject2= jsonArray.getJSONObject(i);
                        String address=jsonObject2.getString("address");
                        String city =jsonObject2.getString("city");
                        String last_4 = jsonObject2.getString("last_4");
                        String merch_name =jsonObject2.getString("merch_name");
                        String  phone = jsonObject2.getString("phone");
                        String state = jsonObject2.getString("state");
                        String tran_id =jsonObject2.getString("tran_id");
                        String txn_amt = jsonObject2.getString("txn_amt");
                        String txn_date = jsonObject2.getString("txn_date");
                        String zip= jsonObject2.getString("zip");
                        group.setMerch_name(merch_name);
                        group.setTxn_amt(txn_amt);
                        group.setTxn_date(txn_date);
                        child.setName(address);
                        ch_list.add(child);
                        list.add(group);

                    }


                }


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

            return histry_result;
        }

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



        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            PD.dismiss();


            ExpAdapter = new ExpandListAdapter(
                    History.this, list);
            ExpandList.setAdapter(ExpAdapter);

        }
    }
}

And my Adapter Class is:

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;


    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

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

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

        Child child = (Child) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_item, null);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.country_name);

        tv.setText(child.getName().toString());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.trns);
        TextView date=(TextView) convertView.findViewById(R.id.dateTime);
        TextView trn_amt=(TextView) convertView.findViewById(R.id.txnmnt);
        tv.setText(group.getMerch_name());
        date.setText(group.getTxn_date());
        trn_amt.setText(group.getTxn_amt());
        return convertView;
    }

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

    }

Upvotes: 1

Views: 345

Answers (2)

Mani
Mani

Reputation: 44

In the below Link you can add the header data in listDataHeader and child data in listDataChild and made some below changes.

HashMap< String, List< Modelclass>> parent;
List<  Modelclass > comingSoon1;
for (int i =0 ;i< jsonArray.length();i++)
{             
     parent = new HashMap<String, List<Modelclass>>();
     comingSoon1 = new ArrayList<Modelclass>();
     Group  group=new Group();
     JSONObject jsonObject2= jsonArray.getJSONObject(i);
     String address=jsonObject2.getString("address");
     String city =jsonObject2.getString("city");
     String last_4 = jsonObject2.getString("last_4");
     String merch_name =jsonObject2.getString("merch_name");
     String  phone = jsonObject2.getString("phone");
     String state = jsonObject2.getString("state");
     String tran_id =jsonObject2.getString("tran_id");
     String txn_amt = jsonObject2.getString("txn_amt");
     String txn_date = jsonObject2.getString("txn_date");
     String zip= jsonObject2.getString("zip");
     group.setMerch_name(merch_name);
     group.setTxn_amt(txn_amt);
     group.setTxn_date(txn_date);
     comingSoon1.add(group);
     parent.put("Your Heading", comingSoon1);        
}

how to add expandable listview inside material design navigation drawer in android?

Upvotes: 1

G. Kalender
G. Kalender

Reputation: 491

You could add a null check to getChildrenCount

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    if (chList != null) {
        return chList.size();
    } else {
        return 0;
    }
}

That would fix the problem when you really have no children for this group. But if you definitely have children and it's still null, you should check the part where you add the child items.

Upvotes: 0

Related Questions