2017-09-15 95 views
0

我用我的代碼使用自定義ExpandableListViewAdapter。當我針對用戶輸入過濾代碼時,應用程序可以正常工作。然而,當用戶刪除表示當輸入被置於輸入列表視圖中留下最後的搜索,然後變成空白,這是我的代碼: 主營:過濾器方法擺脫過濾器後的所有項目

using Android.App; 
using Android.Widget; 
using Android.OS; 
using System.Net; 
using Java.Lang; 
using System; 
using Newtonsoft.Json.Linq; 
using System.Linq; 
using Java.Util; 
using System.Threading; 
using Org.Json; 
using Android.Content; 
using Android.Views; 
using System.Collections.Generic; 
using System.Text; 
using RestSharp.Extensions.MonoHttp; 
using Android.Text; 

namespace DictionaryE 
{ 
[Activity(Label = "DictionaryE", MainLauncher = true, Icon = "@drawable/logo")] 
public class MainActivity : Activity 
{ 
private ExpandableListView list; 
private ExpandableListViewAdapter mAdapter; 
private ArrayAdapter<string> adapter; 
private int length; 
private List<string> group= new List<string>(); 
private string[] names; 
private Dictionary<string, List<string>> Mapout = new Dictionary<string, List<string>>(); 
private SearchView searchBar; 
private View.IOnClickListener listener; 


protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 
    ActionBar.Hide(); 
    // Set Views 
    searchBar = FindViewById<SearchView>(Resource.Id.searchBar); 
    list = FindViewById<ExpandableListView>(Resource.Id.lv); 
    //Set Groups 
    WebClient client = new WebClient(); 
    string json = client.DownloadString("********************"); 
    JSONArray myarray = new JSONArray(json); 
    length = myarray.Length(); 
    names = new string[length]; 
    for (int i = 0; i < length; i++) 
    { 
     JSONObject Element = myarray.GetJSONObject(i); 
     names[i] = Element.GetString("name"); 
    } 
    setData(out mAdapter); 
    list.SetAdapter(mAdapter); 

    mAdapter.Filter.InvokeFilter(); 

} 

private void setData(out ExpandableListViewAdapter mAdapter) 
{ 
    string urlholder; 
    string url; 
    string json; 
    string time; 
    string timestamp; 
    string together; 
    WebClient client1 = new WebClient(); 
    for (int i=0;i < length; i++) 
    { 
     List<string> listplaceholder = new List<string>(); 
     group.Add(names[i]); 
     urlholder = Uri.EscapeDataString(names[i]); 
     url = "**********"; 
     json = client1.DownloadString(url); 
     JSONArray array2 = new JSONArray(json); 
     int length2 = array2.Length(); 
     for (int j = 0; j < length2; j++) 
     { 
      JSONObject Element = array2.GetJSONObject(j); 
      time=Element.GetString("wait"); 
      JSONObject TimeElement = array2.GetJSONObject(j); 
      timestamp = TimeElement.GetString("created_at"); 
      timestamp=timestamp.Replace("T", " at "); 
      int index = timestamp.IndexOf("."); 
      if (index > 0) 
      { 
       timestamp = timestamp.Substring(0, index); 
      } 
      together = time + " minutes posted at " + timestamp; 
      listplaceholder.Add(together); 
     } 
     Mapout.Add(group[i], listplaceholder); 


    } 
    mAdapter = new ExpandableListViewAdapter(this, group, Mapout); 
} 
private void searchBar_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e) 
    { 

     mAdapter.Filter.InvokeFilter(e.NewText); 
    } 

} 
} 

這是我ExpandableListview適配器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Java.Lang; 

namespace BarStar 
{ 
public class ExpandableListViewAdapter : BaseExpandableListAdapter, IFilterable 
{ 
    private Context context; 
    public List<string> listGroup; 
    public Dictionary<string, List<string>> listChild; 

    public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, List<string>> listChild) 
    { 
     this.context = context; 
     this.listGroup = listGroup; 
     this.listChild = listChild; 
    } 
    public override int GroupCount 
    { 
     get 
     { 
      return listGroup.Count; 
     } 
    } 

    public override bool HasStableIds 
    { 
     get 
     { 
      return false; 
     } 
    } 

    public Filter Filter => new GroupFilter(this); 

    public override Java.Lang.Object GetChild(int groupPosition, int childPosition) 
    { 
     var result = new List<string>(); 
     listChild.TryGetValue(listGroup[groupPosition], out result); 
     return result[childPosition]; 
    } 

    public override long GetChildId(int groupPosition, int childPosition) 
    { 
     return childPosition; 
    } 

    public override int GetChildrenCount(int groupPosition) 
    { 
     var result = new List<string>(); 
     listChild.TryGetValue(listGroup[groupPosition], out result); 
     return result.Count; 
    } 

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); 
      convertView = inflater.Inflate(Resource.Layout.Children, null); 
     } 
     TextView textViewItem = convertView.FindViewById<TextView>(Resource.Id.DataValue); 
     string content = (string)GetChild(groupPosition, childPosition); 
     textViewItem.Text = content; 
     return convertView; 
    } 

    public override Java.Lang.Object GetGroup(int groupPosition) 
    { 
     return listGroup[groupPosition]; 
    } 

    public override long GetGroupId(int groupPosition) 
    { 
     return groupPosition; 
    } 

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); 
      convertView = inflater.Inflate(Resource.Layout.Groups, null); 
     } 
     string textGroup = (string)GetGroup(groupPosition); 
     TextView textViewGroup = convertView.FindViewById<TextView>(Resource.Id.Header); 
     textViewGroup.Text = textGroup; 
     return convertView; 
    } 

    public override bool IsChildSelectable(int groupPosition, int childPosition) 
    { 
     return true; 
    } 


} 

}

這是我的過濾等級:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Java.Lang; 

namespace BarStar 
{ 
class GroupFilter : Filter 
{ 
    ExpandableListViewAdapter _adapter; 
    ExpandableListViewAdapter placeholder; 
    public GroupFilter(ExpandableListViewAdapter adapter) 
    { 
     _adapter = adapter; 
    } 
    protected override FilterResults PerformFiltering(ICharSequence constraint) 
    { 
     var returnObject = new FilterResults(); 
     if (constraint == null || constraint.Length() == 0) 
     { 
      // No filter implemented we return all the list 
      returnObject.Values = _adapter; 
      returnObject.Count = _adapter.listGroup.Count(); 
     } 
     else 
     { 

      var tmpList = _adapter.listGroup.Where(g => g.ToLower().Contains(constraint.ToString().ToLower())); 

      returnObject.Values = FromArray(tmpList.ToArray()); 
      returnObject.Count = tmpList.Count(); 
     } 
     return returnObject; 
    } 

    protected override void PublishResults(ICharSequence constraint, FilterResults results) 
    { 
     if (results.Count == 0) 
     { 
      _adapter.NotifyDataSetInvalidated(); 
     } 
     else 
     { 
      _adapter.listGroup = results.Values.ToArray<string>().ToList<string>(); 
      _adapter.NotifyDataSetChanged(); 
      constraint.Dispose(); 
      results.Dispose(); 
     } 
    } 


} 

}

回答

1

然而,當用戶刪除表示當輸入被置於

輸入列表視圖中留下最後的搜索,然後變成空白,你需要做一些改動ExpandableListviewAdapter讓它根據用戶輸入的動態變化:

ExpandableListviewAdapter.cs創建一個局部變量來存儲過濾,當你調用Filter,吸氣不創建一個新的過濾器:

public class ExpandableListViewAdapter : BaseExpandableListAdapter,IFilterable 
{ 
    private GroupFilter _filter; 
    ... 
    public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, List<string>> listChild) 
    { 
     this.context = context; 
     this.listGroup = listGroup; 
     this.listChild = listChild; 
     _filter = new GroupFilter(this); 
    } 
    ... 
    public Filter Filter => _filter; 
} 

然後在你的Filter創建一個新的列表來存儲您listGroup的原始數據:

private class GroupFilter : Filter 
{ 
    List<string> _originalList; 
    ... 
    public GroupFilter(ExpandableListViewAdapter adapter) 
    { 
     _adapter = adapter; 
     _originalList = new List<string>(adapter.listGroup); 
    } 

    ... 
    protected override FilterResults PerformFiltering(ICharSequence constraint) 
    { 
     var returnObject = new FilterResults(); 

     //var tmpList= _originalList.Where(g => g.ToLower().Contains(constraint.ToString().ToLower())); 
     //use LinQ to generate the filter result 
     var tmpList = from o in _originalList where o.ToLower().Contains(constraint.ToString().ToLower()) select o; 

     returnObject.Values = FromArray(tmpList.ToArray()); 
     returnObject.Count = tmpList.Count(); 
     return returnObject; 
    } 
} 
+0

對不起我該怎麼放發佈的結果?因爲_originalList沒有NotifyDataSetChanged();因爲它 –

+0

發佈結果保持不變。我已經更新了演示,請檢查:[TestFilterDemo](https://github.com/elvisxia/TestFilterDemo) –

+0

非常感謝您的幫助! TestFilterDemo是一款救生員 –