Android - 自定义列表视图中的搜索选项c#

时间:2018-06-14 14:33:14

标签: c# android visual-studio listview xamarin

我有一个自定义Listview,其中包含两个textview和一个imageviewlistview包含超过100行,这使得用户更难以查看和选择他的项目。所以我想添加搜索选项,以便用户更容易直接搜索他想要选择的项目。我已在EditText上方添加了ListView,但我不知道下一步该做什么。

Activity.cs:

ListView mListView;
MyAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);
    mListView = FindViewById<ListView>(Resource.Id.listview);
    List<TableList> list = new List<TableList>();
    EditText search = view.FindViewById<EditText>(Resource.Id.searchList);
    list.Add(new TableList("Germany","Europe"));
    list.Add(new TableList("France","Europe"));
    list.Add(new TableList("Finland","Europe"));
    list.Add(new TableList("Germany","Europe"));
    list.Add(new TableList("France","Europe"));
    list.Add(new TableList("Germany","Europe"));
    list.Add(new TableList("France","Europe"));
    list.Add(new TableList("Finland","Europe"));

    adapter = new MyAdapter(this, list);
    mListView.Adapter = adapter;
    mListView.ItemClick += MListView_ItemClick;
}

class MyAdapter : BaseAdapter
{
    Context mContext;
    List<TableList> mitems;
    public MyAdapter(Context context, List<TableList> list)
    {
        this.mContext = context;
        this.mitems = list;

    }
    public override int Count
    {
        get
        {
            return mitems.Count;
        }
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return mitems[position];
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        DataViewHolder holder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.CoinList, null, false);
            holder = new DataViewHolder();
            holder.tv = convertView.FindViewById<TextView>(Resource.Id.CoinName);
            holder.iv = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
            holder.cb = convertView.FindViewById<TextView>(Resource.Id.cont);
            convertView.Tag = holder;
        }
        else
        {
            holder = convertView.Tag as DataViewHolder;

        }
        holder.cb.Tag = position;

        holder.tv.Text = mitems[position].Name;
        holder.cb.Text = mitems[position].Cont
        holder.iv.SetImageResource(Resource.Drawable.dapao);
        return convertView;

    }
}

public class DataViewHolder : Java.Lang.Object
{
    public ImageView iv { get; set; }
    public TextView tv { get; set; }
    public TextView cb { get; set; }

}
public class TableList : Java.Lang.Object
{
    public TableList(string name, string cont)
    {
        this.Name = name;
        this.Cont = cont;
    }
    public string Name { get; set; }
    public string Cont { get; set; }
}
}

所以我希望过滤器位于Name,例如当他在F中写EditTextlistview France只显示Finlandpublic abstract class BaseClass { public int Foo; } public class ClassA : BaseClass { } public class ClassB : BaseClass { public int Bar; } public async Task ProcessVariable(int variable) { BaseClass c = null; // initialized by child class Task t = null; switch (variable) { case 1: c = new ClassA(variable); // parse data for ClassA and BaseClass // potentially some special code here t = OnClassA(c as ClassA, "foo"); break; case 2: c = new ClassB(variable); // parse data for ClassB and BaseClass // potentially some special code here t = OnClassB(c as ClassB); break; } // first call different task than the one picked in switch // can't call it before switch, as variable c is initialized in the switch body if (c != null) await OnAnyClass(c).ConfigureAwait(false); // finally, call the async task we picked inside switch cases if (t != null) await t.ConfigureAwait(false); } public virtual async Task OnAnyClass(BaseClass c) { // this one does something await SendAsync(c.Foo).ConfigureAwait(false); } public virtual async Task OnClassA(ClassA c, string additionalInfo) { // this one does nothing, but end user can override await Task.CompletedTask; } public virtual async Task OnClassB(ClassB c) { // this one does something await SendAsync(c.Bar).ConfigureAwait(false); } 。我怎么能这样做?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您希望使用MultiAutoCompleteTextViewAutoCompleteTextView代替EditText

here查看它们之间的区别。

如果您只想输入一个国家/地区,则可以使用AutoCompleteTextViewhere是官方示例。

更新

我已更新了我demo,下面是结果: enter image description here