如何在ListView数据为空时隐藏线性布局?

时间:2017-04-21 13:18:01

标签: android listview

我想在listview为空时隐藏整个线性布局? 我尝试了setEmptyView,但那件事我不需要。

这里是我的listView代码: -

  @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);
    }
}

这里上面的代码用于listview,当数据为空时我想隐藏线性布局。我不知道如何执行这项任务。我尝试了很多,但没有得到超出输出。

我在这里尝试了setEmptyView我可以在listview为空时放置textview。但我想隐藏整个线性布局。

3 个答案:

答案 0 :(得分:1)

LinearLayout schduletasks;

...

 @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);
schduletasks=(LinearLayout)view.findViewById(R.id.sch);
 new pendingscheduletask().execute();
     return view;

}

 @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);
    if(listAdapter == null) {

        schduletasks.setVisibility(View.Gone);

       } else{



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

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

       }

}

答案 1 :(得分:1)

首先,您要根据所有孩子的身高设置 FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); for (UserInfo profile : user.getProviderData()) { Log.d(TAG, "Provider: " + profile.getProviderId()); } 身高,为什么不将身高设置为ListView

要实现您的目标,您只需在wrap_content中执行此操作:

onPostExecute

答案 2 :(得分:0)

我想象如下:enter image description here

由于您正在从AsyncTask加载数据,因此您可以在已经实现的方法onPostExecute中完成后加载操作,您唯一需要做的就是调用{{1}对于特定的观点。

setVisibility(GONE)

现在您的条件是否适用于适配器是空的,因为依赖于linearLayout1.setVisibility(View.GONE) 上的倍数,您可以使用方法大小来判断您的适配器是否有数据。

旁注:

  • 我通常在onCreate(Activity)或onActivityCreated(Fragment)中设置了一个适配器,但在生命周期中是第一个,然后只需使用确保它将调用的方法修改适配器中的项目notifyDataSetChangednotifyDataSetInvalidated

  • 不要在onBackground方法中调用任何基于UI的方法,如daily_task_detail,而不将其发布到主线程。

  • 您可能知道但AsyncTask已经取而代之的是其他alternatives,IntentServices,RxAndroid,Sync。