【问题标题】:Is it possible to pass view to ListView Adapter class?是否可以将视图传递给 ListView 适配器类?
【发布时间】:2015-09-16 09:33:34
【问题描述】:

我的布局中有一个 TextView 和一个 ListView。我正在执行过滤操作,结果在 ListView 中设置。如果结果为 null,那么我想在我的 TextView 中设置“无结果”。 是否可以通过 Adapter 类的构造函数传递 TextView 以便在结果为 null 时设置文本?

【问题讨论】:

  • 因此,当结果为无时,您可以在布局中维护一个 textview,将 textview 可见性设置为 VISIBLE else GONE

标签: android listview android-filter


【解决方案1】:

您可以使用setEmptyView()

  1. 在您的布局中,将TextView 的可见性设置为android:visibility="gone"
  2. 在您的活动中,使用setEmptyView()TextView 的实例设置为ListView

    ListView listView = (ListView) findViewById(R.id.your_list_view);
    listView.setEmptyView(findViewById(R.id.your_empty_text_view));
    

【讨论】:

    【解决方案2】:

    我通过构造函数传递了我的 TextView,并在 publishResults 中检查了结果并相应地设置了可见性

    @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
    
            try{
            filteredModelItemsArray=new ArrayList<MarkerRowItem>();
            filteredModelItemsArray = (ArrayList<MarkerRowItem>) results.values;
    
            if(results != null && results.count > 0) {
                notifyDataSetChanged();
                noResultTextView.setVisibility(View.GONE);
            } else {
                notifyDataSetInvalidated();
                noResultTextView.setVisibility(View.VISIBLE);
            }
            }
    
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2020-11-08
      • 1970-01-01
      • 2012-01-01
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2021-11-12
      相关资源
      最近更新 更多