Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Created April 21, 2012 01:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conceptdev/2433227 to your computer and use it in GitHub Desktop.
Save conceptdev/2433227 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
namespace ContentControls {
public class AutoAdapter : ArrayAdapter, IFilterable
{
LayoutInflater inflater;
Filter filter;
Activity context;
public string[] AllItems;
public string[] MatchItems;
public AutoAdapter (Activity context, int txtViewResourceId, string[] items)
: base(context, txtViewResourceId, items)
{
inflater = context.LayoutInflater;
filter = new SuggestionsFilter(this);
AllItems = items;
MatchItems = items;
}
public override int Count {
get {
return MatchItems.Length;
}
}
public override Java.Lang.Object GetItem (int position)
{
return MatchItems[position];
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = inflater.Inflate(Android.Resource.Layout.SimpleDropDownItem1Line, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = MatchItems[position];
return view;
}
public override Filter Filter {
get {
return filter;
}
}
class SuggestionsFilter : Filter
{
AutoAdapter a;
public SuggestionsFilter (AutoAdapter adapter) : base() {
a = adapter;
}
protected override Filter.FilterResults PerformFiltering (Java.Lang.ICharSequence constraint)
{
FilterResults results = new FilterResults();
if (constraint != null) {
var searchFor = constraint.ToString ();
Console.WriteLine ("searchFor:" + searchFor);
var matchList = new List<string>();
var matches = from i in a.AllItems
where i.IndexOf(searchFor) >= 0
select i;
foreach (var match in matches) {
matchList.Add (match);
}
a.MatchItems = matchList.ToArray ();
Console.WriteLine ("resultCount:" + matchList.Count);
Java.Lang.Object[] matchObjects;
matchObjects = new Java.Lang.Object[matchList.Count];
for (int i = 0; i < matchList.Count; i++) {
matchObjects[i] = new Java.Lang.String(matchList[i]);
}
results.Values = matchObjects;
results.Count = matchList.Count;
}
return results;
}
protected override void PublishResults (Java.Lang.ICharSequence constraint, Filter.FilterResults results)
{
a.NotifyDataSetChanged();
}
}
}
}
using System.Collections.Generic;
using System.IO;
using Android.App;
using Android.OS;
using Android.Widget;
namespace ContentControls {
[Activity(Label = "AutoCompleteTextView")]
public class AutoCompleteTextViewScreen : Activity {
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.AutoCompleteTextView);
AutoCompleteTextView act = FindViewById<AutoCompleteTextView>(Resource.Id.AutoCompleteInput);
Stream seedDataStream = Assets.Open(@"WordList.txt");
//StreamReader reader = new StreamReader(seedDataStream);
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(seedDataStream)) {
// 3
// Use while != null pattern for loop
string line;
while ((line = reader.ReadLine()) != null) {
// 4
// Insert logic here.
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
}
string[] wordlist = lines.ToArray();
act.Adapter = new AutoAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, wordlist);
}
}
}
@deepakthumbmunkeys
Copy link

I'm trying to implement an autocomplete textview as same like you but item list is keep on updating at run time.. can you please tell me how it will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment