Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Android - Search option in custom listview c#

I have a custom Listview containing two textview and one imageview. The listview contains more than 100 rows which makes it more difficult to the user to view and select his items. So i want to add the search option in order to make it easier for the user to directly search for the items he want to select. I have already added an EditText above the ListView but I don't have any clue what to do next.

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

So I want the filter to be by the Name for example when he writes the letter F in the EditText the listview should shows only France and Finland. How can I do so ? Please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would like to use MultiAutoCompleteTextView or AutoCompleteTextView instead of EditText.

See the difference between them from here.

If you just want to input one country, you can use AutoCompleteTextView, here is the official example.

update:

I have updated me demo, below is result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...