I'm trying to setup a filter for my list view but I've come across these errors.
Cannot resolve symbol 'myAdapter'
I know that myAdapter
hasn't been initialised, but I don't know which of my classes I need to declare it in.
error: method setOnQueryTextListener in class SearchView cannot be applied to given types; required: android.support.v7.widget.SearchView.OnQueryTextListener found: android.widget.SearchView.OnQueryTextListener reason: actual argument android.widget.SearchView.OnQueryTextListener cannot be converted to android.support.v7.widget.SearchView.OnQueryTextListener by method invocation conversion
I know that android.widget.SearchView.OnQueryTextListener
cannot be converted to android.support.v7.widget.SearchView.OnQueryTextListener
but I don't know what needs to change in my code. My app utilises the Support library. All help would be appreciated.
VictoriaLineActivity.java
public class VictoriaLineActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
//change background colour of action bar
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0098D4")));
//change text colour of action bar
actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'>Victoria line</font>"));
//enable and show action bar back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
FragmentVictoriaLine newFragment = new FragmentVictoriaLine();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
}
}
FragmentVictoriaLine.java
public class FragmentVictoriaLine extends android.support.v4.app.Fragment implements {
private class Victoria {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public Victoria(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
@Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static Victoria[] mVictoria;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
public boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_victoria_line, container, false);
if (getActivity().findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}else{
mTwoPane = false;
}
// Instantiate the list of stations.
mVictoria = new Victoria[]{
new Victoria(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new Victoria(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_victoria);
listView.setAdapter(new MyAdapter(getActivity(), mVictoria));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setTextFilterEnabled(true);
});
return v;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_victoria_line, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
android.support.v7.widget.SearchView.OnQueryTextListener textChangeListener = new android.support.v7.widget.SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
myAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
myAdapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
Victoria[] mVictoria;
public MyAdapter(Context contexts, Victoria[] samples) {
this.mVictoria = samples;
inflater = LayoutInflater.from(contexts);
}
@Override
public int getCount() {
return mVictoria.length;
}
@Override
public Object getItem(int position) {
return mVictoria[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mVictoria[position].station);
viewHolder.zone.setText(mVictoria[position].getzone());
return convertView;
}
}
}
Your errors pretty much tell you what you are doing wrong:
That is because you have not declared any variable called
myAdapter
Check your imports. You need to use
android.support.v7.widget.SearchView
but you are using the standardSearchView
. This should be fixable by simply importing the correct package.Edit:
You are using the wrong type of
OnQueryListener
you need to use the support version.Edit 2: Hopefully this will help you get started:
Edit 3:
Here is the full demo: https://github.com/nak411/AndroidFilterListDemo