-1

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.

Wassim01
  • 189
  • 3
  • 11

1 Answers1

0

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

Community
  • 1
  • 1
Robbit
  • 4,300
  • 1
  • 13
  • 29
  • That's not the problem. The problem is the listview. I want only to display in it the searched item. Like if the user searched for `France`, it should ONLY display the row containing France – Wassim01 Jun 15 '18 at 01:58
  • Sorry, I have misread you, you want your `ListView` scroll to the `France` or you want your `ListView` only show the `France`? – Robbit Jun 15 '18 at 02:08
  • What about [this](https://stackoverflow.com/questions/12456525/how-to-filter-listview-using-getfilter-in-baseadapter)? `adapter.getFilter().filter(s);` You need custom a Filter for you Adapter. – Robbit Jun 15 '18 at 02:13
  • And [here](https://stackoverflow.com/a/24771174/8632294) is a example which is more completely. – Robbit Jun 15 '18 at 02:20
  • I have a small problem, I am getting an error here: `results.Values = resultsValues;`in the `FilterResults PerformFiltering(ICharSequence constraint)` function, the error is:`Cannot implicitly convert type TableList to Java.Lang.Object` ?? – Wassim01 Jun 15 '18 at 20:56
  • And a `null exception` here: `var arry = results.Values.ToArray();` – Wassim01 Jun 15 '18 at 21:25
  • I don't have these problems on my phone, has the project run on your phone once? – Robbit Jun 18 '18 at 00:56
  • `Cannot implicitly convert type TableList to Java.Lang.Object`, your `TableList` need implement `Java.Lang.Object`. `var arry = results.Values.ToArray();` check the value of `results.Values`. – Robbit Jun 18 '18 at 01:02