我有一个数据库,我正在使用填充列表视图。在这个listview之上有一个edittext,用户在listview中搜索一个项目。根据输入,我希望对listview进行优化和过滤,以仅包含那些与输入类似的项目。这在java中很简单,但在xamarin的android中很难。这里是我的onCreate()方法,其中数据库填充列表视图。在单声道为Android的AddTextChangedListener
string[] categories;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.b);
var destPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DDxDDB");
System.IO.Stream source = Assets.Open("DDxDDB");
var dest = System.IO.File.Create (destPath);
source.CopyTo (dest);
var sql = "SELECT _id FROM Sx;";
var conn = new SqliteConnection ("Data Source=" + destPath.ToString());
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = sql;
SqliteDataReader reader = cmd.ExecuteReader();
List<string> categ = new List<string>();
while (reader.Read())
{
categ.Add(reader.GetString(0));
}
categories = categ.ToArray();
ArrayAdapter<string> dataAdapter = new ArrayAdapter<String>(this, Resource.Layout.Main, categories);
ListView listView = (ListView) FindViewById(Resource.Id.listView1);
listView.Adapter = dataAdapter;
listView.TextFilterEnabled = true;
EditText myFilter = (EditText)FindViewById(Resource.Id.myFilter);
myFilter.AddTextChangedListener(New MyTextWatcher);
}
这是类,我还包括在本次活动,但我不知道什么添加到代码的方法,以使我的列表视图的过滤。
public class MyTextWatcher: Java.Lang.Object, ITextWatcher
{
public void AfterTextChanged(IEditable s) {}
public void BeforeTextChanged(Java.Lang.ICharSequence arg0, int start, int count, int after) {}
public void OnTextChanged(Java.Lang.ICharSequence arg0, int start, int before, int count) {}
}
你是怎么在Android中做这件事的?也许发布你在那里做的等效代码,那么我会很容易地引导你如何在Mono for Android上做到这一点。 – Cheesebaron
这是我一直使用的例子。我的困难是可以在java中的其他方法而不是c#中定义方法。 http://stackoverflow.com/questions/5311032/filter-text-from-listview – user1518003