【问题标题】:Search Bar for an ExpandableListViewExpandableListView 的搜索栏
【发布时间】:2017-09-12 07:00:42
【问题描述】:

在我的代码中,我使用自定义 ExpandableListViewAdapter,使用 BaseExpandableListAdapter。当有人在搜索栏中搜索特定单词时,我正在尝试过滤我的父组。我试图使用 ITextWatcher 但是,当我将 .Filter 与我的 ExpandableListViewAdapter mAdapter 一起使用时,我的代码将无法构建。我也不能使用 InvokeFilter(s) 过滤掉父母。有人可以帮忙吗?提前谢谢!

我再次尝试过滤父母;不是孩子。这是我的主要内容:

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

}
}

这是我的自定义可扩展基本适配器:

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 DictionaryE
{
public class ExpandableListViewAdapter : BaseExpandableListAdapter
{
    private Context context;
    private List<string> listGroup;
    private 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 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;
    }


}

}

【问题讨论】:

  • 您能发布您的代码吗?对于when I use .Filter with my ExpandableListViewAdapter mAdapter my code wont build.,你有什么例外吗?在哪一行?
  • 当然我可以发布代码;例外是 IFilter 是在给定上下文中无效的方法。

标签: listview search xamarin xamarin.android expandablelistview


【解决方案1】:

Filter 是一种在给定上下文中无效的方法。

要在你的适配器上使用过滤器,你需要让你的适配器实现IFilterable接口:

public class ExpandableListViewAdapter : BaseExpandableListAdapter,IFilterable
{
  ...
}

更新:

如果你已经实现了接口,默认情况下Filter 为空,如下所示:

public class ExpandableListViewAdapter : BaseExpandableListAdapter,IFilterable
{
  ...
  Filter=>throw new NotImplementedException();
}

这就是您收到异常的原因。需要做的是创建一个自定义的Filter类:

public class GroupFilter:Filter
{
    ExpandableListViewAdapter _adapter;
    public GroupFilter(ExpandableListViewAdapter adapter)
    {
        _adapter = adapter;
    }
    protected override FilterResults PerformFiltering(ICharSequence constraint)
    {
        var result = new FilterResults();
        // add the filtered items to FilterResults
        //convert net object to java object
        return result;
    }

    protected override void PublishResults(ICharSequence constraint, FilterResults results)
    {
        //convert java object to Net object
        //Call _adapter.NotifyDataSetChanged();
    }
}

并在您的适配器中初始化过滤器:

public class ExpandableListViewAdapter : BaseExpandableListAdapter,IFilterable
{
  ...
  public Filter Filter => new GroupFilter(this);
}

Filer的完整实现示例,可以参考Cheesebaron的demohere

【讨论】:

  • 我在看那个,但是我在方法中添加了什么?那是我感到困惑的地方。我打算使用 adapter.Filter.InvokeFilter(e.NewText);现在当我使用它时它不起作用,因为适配器中的过滤器方法除了 throw new NotImplementedException();
  • 感谢您的回答!但是我迷失在cheesebarons,因为他是一个listview,而我的是一个带有字符串的可扩展列表视图。我正在尝试解决它,但我遇到了很多错误。
  • 抱歉,如果你有时间可以给我看一下它的概要吗?我不断收到错误,并且遇到了困难。
  • 抱歉回复晚了,我给你做一个基本的demo。你能告诉我你想要过滤什么吗?我的意思是,你的过滤规则是什么。
  • 你可以在这里找到我用你的代码创建的演示:TestFilterDemo。请不要直接复制代码,因为我只是创建了假数据供使用。
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多