Correct way and location to initialise adapter

805 views Asked by At

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.OnQueryTextListenercannot 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;
        }
    }
}
1

There are 1 answers

13
Naveed On BEST ANSWER

Your errors pretty much tell you what you are doing wrong:

Cannot resolve symbol 'myAdapter'

That is because you have not declared any variable called myAdapter

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

Check your imports. You need to use android.support.v7.widget.SearchView but you are using the standard SearchView. 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:

public class FragmentVictoriaLine extends Fragment {
    /**Declare other variables**/

    private MyAdapter myAdapter;

    public FragmentVictoriaLine() {
        // Required empty public constructor
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_victoria_line, container, false);

        //Tell the system to call onCreateOptinsMenu
        setHasOptionsMenu(true);
        if (getActivity().findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        } else {
            mTwoPane = false;
        }

        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)
        };

        //Declare your adapter here so you can access later
        myAdapter = new MyAdapter(getActivity(), mVictoria);

        final ListView listView = (ListView) v.findViewById(R.id.list_victoria);
        //Set teh adapter here.
        listView.setAdapter(myAdapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setTextFilterEnabled(true);

        return v;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu items for use in the action bar
        inflater.inflate(R.menu.menu_victoria_line, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconifiedByDefault(false);

        SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextChange(String newText)
            {
                // this is your adapter that will be filtered
                myAdapter.getFilter().filter(newText);
                System.out.println("on text chnge text: "+newText);
                return true;
            }
            @Override
            public boolean onQueryTextSubmit(String query)
            {
                // this is your adapter that will be filtered
                myAdapter.getFilter().filter(query);
                System.out.println("on query submit: "+query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(textChangeListener);

    }

   /**....rest of your code*/
}

Edit 3:

Here is the full demo: https://github.com/nak411/AndroidFilterListDemo