MohanRaj S MohanRaj S - 1 month ago 16
Android Question

Android PopupWindow did not Show Expandable ListView?

Popup window did not show the Expandable ListView,I have used Hasmap array list for parsing JSON API. Perfectly i get Values and assigned to ArrayList.Please guide me to resolve this issue. why PopWindow did not show the ExpandableListView.Thanks in Advance.

public class UserProfile_Add_Education extends AppCompatActivity implements View.OnClickListener{
private EditText edTxt_region,edTxt_orgnicat,edTxt_qualifprov,edTxt_titlequalifi,edTxt_ncea,edTxt_elaboratequalif,edTxt_qualifcat,
edTxt_qualifsubcat,edTxt_startdate,edTxt_endate,edTxt_edu_status;

private NestedScrollView nscrl_addeducation;
private Calendar mCalendar;
String upkeyvalues="";
private DisplayMetrics outMetrics;
private boolean isPortrait;
private int screenHeight;
private int screenWidth;
PopupWindow mPopUpMaster;

private PopupWindow mpopup;

ArrayList<HashMap<String, String>> hs_EduStatus_ArrLst = new ArrayList<HashMap<String, String>>();
HashMap<String,List<String>> hashMap = new HashMap<String, List<String>>();
List<String>list = new ArrayList<String>();

ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail= new HashMap<String, List<String>>();

private ProgressDialog progress;
final int HAND_EDU =1;
final int HAND_ORG_SUB =2;
final int HAND_QLFY_SUB =3;
private int qulifyCatId,regionalId,orgId,qulifyProviderId;
ExpandableListView expListView;
CheckBox chkChild,chkParent;
Button btnSubmit;
TextView txtVw_selected;


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

@Override
public void onClick(View v) {
Intent redirectIntent;
switch(v.getId()){
case R.id.edTxt_qualifsubcat:
try {

PopupwithExpndLst ();

} catch (Exception e) {e.printStackTrace();}
break;

}
}

public void PopupwithExpndLst ()
{

try {
// inflating popup layout
View popUpView = getLayoutInflater().inflate(R.layout.pw_explist, null);
// Creation of popup
mpopup = new PopupWindow(popUpView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
// Displaying popup
mpopup.showAsDropDown(popUpView, 0, 0);
// get the listview
expListView = (ExpandableListView) mpopup.getContentView().findViewById(R.id.lstExpList);
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expListView.setAdapter(expandableListAdapter);

} catch (Exception ex) { Log.i("Test", "Error is " + ex.getMessage());}

}

private class QualificationSubCat extends AsyncTask<Void, Void, Integer>{
String mQulifyCatId= String.valueOf(qulifyCatId);
@Override
protected void onPreExecute() {
progress = new ProgressDialog(UserProfile_Add_Education.this);
progress.setMessage("Loading ...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.setProgress(0);
progress.show();
}

@Override
protected Integer doInBackground(Void... params) {
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("category_id", mQulifyCatId));
NetworkAccessLayer networkAccessLayer = new NetworkAccessLayer();
String jsonStr = networkAccessLayer.partnerSubCat(BusinessAccessLayer.SERVER_IP + "youth/getqualificationsubcategory", NetworkAccessLayer.POST, nameValuePairs,userToken);
mStatus = jsonStr;
} catch (Exception e) {e.printStackTrace();}


return 0;
}

@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
progress.dismiss();
mHandler.sendEmptyMessage(HAND_QLFY_SUB);

}
}



protected Handler mHandler = new Handler(new Handler.Callback() {

@Override
public boolean handleMessage(android.os.Message msg) {
switch (msg.what) {

case HAND_QLFY_SUB:
try {
if (mStatus != null) {
try {
JSONObject c = new JSONObject(mStatus);
String tempStr = c.getString("status").toString();
if (tempStr.equalsIgnoreCase("1") || tempStr.equalsIgnoreCase("true")) {
loadSubQualifationArrayValues(mStatus);
}
} catch (Exception e) {
e.printStackTrace();
}


} else {
NetworkAccessLayer.showSnackBar(nscrl_addeducation, "No data found");
}

} catch (NullPointerException e) {
e.printStackTrace();
}
break;}
return false;
}


});

private void loadSubQualifationArrayValues(String responseStatus ) throws JSONException {
expandableListDetail.clear();
list.clear();
JSONObject jsonObject = new JSONObject(responseStatus);
String status = jsonObject.getString("status");
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONArray jsonArray = dataObject.getJSONArray("subcategory_ids");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONArray jsonArray1 = jsonObject1.getJSONArray("subsubcategory");
for (int j=0;j<jsonArray1.length();j++){
JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
list.add(jsonObject2.getString("subcategory"));}
expandableListDetail.put(jsonObject1.getString("subcategory"),list);
System.out.println("Qfy_SubCat Size:"+expandableListDetail.size());
}

}

}

Answer

Please Stay Calm and read myCode.I have answer to this question, here by i add required functionality to show api values in Expandable list view. i was used List and HashMap to achieve the result. Hope it will help for some like me facing this kind of issue. Thank-you.

Necessary Variables,Methods and Class below,

public class ExpList_PopWindowsAns extends AppCompatActivity implements View.OnClickListener {

//required DataHandling components
    List<String> list = new ArrayList<String>();
    ExpandableListAdapter expandableListAdapter;
    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
//oncreate
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        EditText edTxt_qualifsubcat = (EditText) findViewById(R.id.edTxt_qualifsubcat);
        edTxt_qualifsubcat.setOnclickListener(this);
        getDisplayDetails();
        if (getScreenWidth() > getScreenHeight()) {
            isPortrait = false;
        } else if (getScreenHeight() > getScreenWidth()) {
            isPortrait = true;
        }


    }
//Display details

protected void getDisplayDetails() {

        Display display = getWindowManager().getDefaultDisplay();
        outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        screenHeight = outMetrics.heightPixels;
        screenWidth = outMetrics.widthPixels;
    }

    public int getWidthByPercentage(double i) {

        int result = (int) ((i * screenWidth) / 100);
        return result;
    }

    protected float getScreenWidth() {
        return screenWidth;
    }

    protected float getScreenHeight() {
        return screenHeight;
    }

    public int getHeightByPercentage(double d) {

        int result = (int) ((d * screenHeight) / 100);
        return result;
    }

    @Override
    public void onClick(View v) {
        Intent redirectIntent;
        switch (v.getId()) {
            case R.id.edTxt_qualifsubcat:

                if (expandableListDetail.size() > 0) {

                    try {

                        PopupwithExpndLst();

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

                    }
                } else {
                   //Do your Stuff
                }
                break;
            }
        }

//Structue the Popwindow with Expandable List
//Calculate the width and height
public void PopupwithExpndLst() {
        if (expandableListDetail.size() > 0) {
            try {
                // inflating popup layout
                View popUpView = getLayoutInflater().inflate(R.layout.pw_explist, null);
                // Creation of popup
                mpopup = new PopupWindow(popUpView, isPortrait ? getHeightByPercentage(32) : getWidthByPercentage(50), isPortrait ? getHeightByPercentage(40) : getWidthByPercentage(30), true);
                // Displaying popup
                mpopup.showAtLocation(popUpView, Gravity.LEFT, getHeightByPercentage(5), getHeightByPercentage(100));
                // get the listview
                expListView = (ExpandableListView) mpopup.getContentView().findViewById(R.id.lstExpList);
                expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
                expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
                expListView.setAdapter(expandableListAdapter);

                expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {

                        //To set the Text values in Editext
                        edTxt_qualifsubcat.setText(expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition));
                        //To set the Id for API purpose
                        qulifySubCatId = childPosition;
                        return false;
                    }
                });

            } catch (Exception ex) {
                Log.i("Test", "Error is " + ex.getMessage());
            }
        } else {
            //Do your Stuff
        }
    }

//Parse the JSON API using the Following Steps After Sucessfull AsyncTask  API Call
 private void loadSubQualifationArrayValues(String responseStatus) throws JSONException {
        expandableListDetail.clear();//clear HashMap for avoiding the extra adding information
        list.clear();//clear list for avoiding the extra adding information
        JSONObject jsonObject = new JSONObject(responseStatus);
        String status = jsonObject.getString("status");//place your api keys
        JSONObject dataObject = jsonObject.getJSONObject("data");//place your api keys
        JSONArray jsonArray = dataObject.getJSONArray("subcategory_ids");//place your api keys

        for (int i = 0; i < jsonArray.length(); i++) {
            list = new ArrayList<String>();

            JSONObject jsonObject1 = jsonArray.getJSONObject(i);

            JSONArray jsonArray1 = jsonObject1.getJSONArray("subsubcategory");//place your api keys

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

                JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                list.add(jsonObject2.getString("subcategory"));//place your api keys
            }

            expandableListDetail.put(jsonObject1.getString("subcategory"), list);//place your api keys

            System.out.println("Qfy_SubCat Size:" + expandableListDetail.size());
        }

    }

}

CustomExpandableListAdapter

    public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;

    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

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

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.pw_list_item, null);
        }
        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.pw_list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
        //listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        listTitleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? R.drawable.ic_expand_less_24dp : R.drawable.ic_expand_more_24dp, 0);
        /*listTitleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : android.R.drawable.ic_menu_more, 0);
        listTitleTextView.setText(getGroup(listPosition).toString());*/
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

Thanks Again. I expecting comments.