Search code examples
androidlistview

How to hide Linear layout when ListView data is empty?


I want to hide whole Linear layout when listview is empty? I tried setEmptyView but that thing i dont needed.

Here my listView code :-

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_notification_task, container, false);
    listschedule=(ListView)view.findViewById(R.id.notificationtaskschedule);
    LinearLayout schduletasks=(LinearLayout)view.findViewById(R.id.sch);
     new pendingscheduletask().execute();
         return view;
}
//daily task pending
private class pendingdailytask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setTitle("Please Wait");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }
    @Override
    protected Void doInBackground(Void... args) {
        try {
            arraylist = new ArrayList<HashMap<String, String>>();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", suid));
            JSONObject json = jParser.makeHttpRequest(url_psendingdailytask, "GET", params);

            int success1 = Integer.parseInt(json.getString("success4"));
            Log.d("success4", json.toString());
            if (success1 == 0) {
                Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
            }
            if (success1 == 1) {
                ownerObj = json.getJSONArray("daily");
                for (int i = 0; i < ownerObj.length(); i++) {
                    jsonobject = ownerObj.getJSONObject(i);

                    daily_task_name.add(jsonobject.getString("t_tname"));
                    daily_task_time.add(jsonobject.getString("t_time"));
                    daily_task_detail.add(jsonobject.getString("t_detail"));
                    daily_task_id.add(jsonobject.getString("t_id"));
                }
            }
        }
        catch(Exception e)
        {}
        return null;
    }
    @Override
    protected void onPostExecute(Void args) {
        listadapter = new ListViewAdapterPendingdailytask(getActivity(), daily_task_name,daily_task_time,daily_task_detail,daily_task_id);
        listdaily.setAdapter(listadapter);

        Utility.setListViewHeightBasedOnChildren(listdaily);
    }
}
public static class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

here the above code is for listview when the data is empty i want to hide Linear Layout. i don't know how to perform this task. i tried a lot but not getting the excet output.

i Tried setEmptyView in this i can put a textview when the listview is empty. but i want to hide whole linear layout.


Solution

  • First of all, you are setting your ListView height based in all it's children height, why don't you just set its height to wrap_content?

    To Achieve what you want you just need to do this in onPostExecute:

    @Override
    protected void onPostExecute(Void args) {
        //in this if clause make your logic between OR/AND depending on what you want
        if(daily_task_name.size() != 0 || daily_task_time.size != 0 || ....){ 
            listadapter = new ListViewAdapterPendingdailytask(getActivity(), daily_task_name,daily_task_time,daily_task_detail,daily_task_id);
            listdaily.setAdapter(listadapter);
            Utility.setListViewHeightBasedOnChildren(listdaily);
        } else{
            //assuming this is the LinearLayout you want to be gone
            schduletasks.setVisibility(View.GONE);
        }
    }