使用xamarin android

时间:2016-03-27 12:48:08

标签: c# android listview xamarin adapter

我正在尝试使用edittext实现即时搜索功能。我刚刚将json数组绑定绑定到listview并在listview顶部添加了edittext并尝试过滤或搜索listview中的数据,因为用户开始在代码下面输入edittext used.Please帮帮我。任何建议,指导和帮助表示赞赏。

MainActivity.cs

  SetContentView(Resource.Layout.HomeScreen);
                tableItems = new List<TableItem>();

                var client = new RestClient("http://azurewebsites.net/");
                var request = new RestRequest("Service/regionSearch", Method.POST);
                request.RequestFormat = DataFormat.Json;
                tableItems = client.Execute<List<TableItem>>(request).Data;

                listView.Adapter = new HomeScreenAdapter(this, tableItems);
                 region = FindViewById<TextView> (Resource.Id.viewtext);
     area= FindViewById<TextView> (Resource.Id.viewtext2);
                _filterText = FindViewById<EditText>(Resource.Id.search);
                listView = FindViewById<ListView>(Resource.Id.listView);
 _filterText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
                // filter on text changed
                var searchTerm = _filterText.Text;
};    


        listView.ItemClick += OnListItemClick;
    }

    protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
    {
        var listView = sender as ListView;
         var t = tableItems[e.Position];
    //   var clickedTableItem = listView.Adapter[e.Position];
        Android.Widget.Toast.MakeText(this, clickedTableItem.DDLValue, Android.Widget.ToastLength.Short).Show();

    }

HomeScreenAdapter.cs

public class HomeScreenAdapter : BaseAdapter<TableItem> {
    List<TableItem> items;
    Activity context;
    public HomeScreenAdapter(Activity context, List<TableItem> items)
        : base()
    {

            this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override TableItem this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
             var item = items[position];

          //  TableItem item = items[position];


            View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.DDLValue;
       view.FindViewById<TextView>(Resource.Id.Text2).Text = item.areaMsg;
          return view;
    }
}

1 个答案:

答案 0 :(得分:1)

看起来你很亲密。最后一步是使用searchTerm过滤掉tableItems中的结果。最简单的方法是使用筛选列表创建新的HomeScreenAdapter,并将其设置为ListView.Adapter。查看实现的示例代码:获取搜索文本,过滤所有TableItem实例,然后为ListView提供新的适配器。

 _filterText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
            // filter on text changed
            var searchTerm = _filterText.Text;
            var updatedTableItems = tableItems.Where(
                // TODO Fill in your search, for example:
                tableItem => tableItem.Msg.Contains(searchTerm) ||
                             tableItem.DDLValue.Contains(searchTerm)
            ).ToList();
            var filteredResultsAdapter = new HomeScreenAdapter(this, updatedTableItems);
            listView.Adapter = filteredResultsAdapter;
 };    

注意Where子句中的TODO。我不知道你想如何搜索你的TableItem但是一旦你写了Where子句,这应该做你想要的。

看起来你的TableItem类是这样的(供参考):

public class TableItem {
    public int Id {get; set;}
    public string DDLValue {get; set;}
    public string Msg {get; set;}
    public int Status {get; set;}
}