_adapater 在当前上下文中不存在
_adapater does not exist in the current context
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var product = new[] {
"India","Nokia","Dhurv","Gohil","Patel","Sony","Bhaumik","Umang","Riya","Afghanistan","America"
};
var _listView = FindViewById<ListView>(Resource.Id.MylistView);
var _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, product);
_listView.Adapter = _adapter;
}
public override bool OnCreateOptionsMenu (IMenu menu)
{
MenuInflater.Inflate (Resource.Layout.Menu,menu);
var item = menu.FindItem(Resource.Id.action_search);
var searchView = MenuItemCompat.GetActionView (item);
var _searchView = searchView.JavaCast<SearchView>();
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
_searchView.QueryTextSubmit += (s, e) =>
{
// Handle enter/search button on keyboard here
Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
e.Handled = true;
};
return true;
}
我收到错误:
_adapter does not exist in current context (in OnCreateOptionsMenu)
如何解决这个错误?
我没有使用 AppCompat
作为 SearchView
。代码可以吗?有办法解决这个问题吗?
您无法在 OnCreateOptionsMenu
中访问 _adapter
,因为它仅在 OnCreate
中已知。要在 class 的其他方法中访问它,您需要将其设为 class 成员。也许有点像这样:
class MyClass
{
ArrayAdapter<String> _adapter = null;
...
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// ... the rest of your code
_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, product);
// ...
}
protected override void OnCreateOptionsMenu(IMenu menu)
{
// ... other stuff
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
// ...
}
}
注意OnCreate
中_adapter = ...
前面没有var
。
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var product = new[] {
"India","Nokia","Dhurv","Gohil","Patel","Sony","Bhaumik","Umang","Riya","Afghanistan","America"
};
var _listView = FindViewById<ListView>(Resource.Id.MylistView);
var _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, product);
_listView.Adapter = _adapter;
}
public override bool OnCreateOptionsMenu (IMenu menu)
{
MenuInflater.Inflate (Resource.Layout.Menu,menu);
var item = menu.FindItem(Resource.Id.action_search);
var searchView = MenuItemCompat.GetActionView (item);
var _searchView = searchView.JavaCast<SearchView>();
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
_searchView.QueryTextSubmit += (s, e) =>
{
// Handle enter/search button on keyboard here
Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
e.Handled = true;
};
return true;
}
我收到错误:
_adapter does not exist in current context (in OnCreateOptionsMenu)
如何解决这个错误?
我没有使用 AppCompat
作为 SearchView
。代码可以吗?有办法解决这个问题吗?
您无法在 OnCreateOptionsMenu
中访问 _adapter
,因为它仅在 OnCreate
中已知。要在 class 的其他方法中访问它,您需要将其设为 class 成员。也许有点像这样:
class MyClass
{
ArrayAdapter<String> _adapter = null;
...
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// ... the rest of your code
_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, product);
// ...
}
protected override void OnCreateOptionsMenu(IMenu menu)
{
// ... other stuff
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
// ...
}
}
注意OnCreate
中_adapter = ...
前面没有var
。