正确的方式和位置初始化适配器

时间:2015-06-17 17:45:33

标签: java android android-activity android-adapter android-listfragment

我尝试为列表视图设置过滤器,但我遇到了这些错误。

  

无法解析符号' myAdapter'

我知道var total = document.getElementsByName("total")[0]; total.value = parseFloat(document.getElementsByName("price")[0].value) + parseFloat(document.getElementsByName("ttaxes")[0].value); 尚未初始化,但我不知道我需要向哪些课程申报。

  

错误:类SearchView中的方法setOnQueryTextListener无法应用于给定的类型;   必需:android.support.v7.widget.SearchView.OnQueryTextListener   发现:android.widget.SearchView.OnQueryTextListener   原因:实际参数android.widget.SearchView.OnQueryTextListener无法通过方法调用转换转换为android.support.v7.widget.SearchView.OnQueryTextListener

我知道myAdapter无法转换为android.widget.SearchView.OnQueryTextListener,但我不知道代码中需要更改的内容。我的应用程序使用支持库。所有的帮助将不胜感激。

VictoriaLineActivity.java

android.support.v7.widget.SearchView.OnQueryTextListener

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

1 个答案:

答案 0 :(得分:2)

你的错误几乎告诉你你做错了什么:

  

无法解析符号'myAdapter'

那是因为你没有声明任何名为myAdapter

的变量
  

错误:类SearchView中的方法setOnQueryTextListener不能   适用于给定类型;需要:   找到android.support.v7.widget.SearchView.OnQueryTextListener:   android.widget.SearchView.OnQueryTextListener原因:实际参数   android.widget.SearchView.OnQueryTextListener无法转换为   android.support.v7.widget.SearchView.OnQueryTextListener方法   调用转换

检查您的进口商品。您需要使用android.support.v7.widget.SearchView,但您使用的是标准SearchView。只需导入正确的包即可解决此问题。

编辑:

您使用的是OnQueryListener的错误类型,需要使用支持版本。

编辑2: 希望这有助于您入门:

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*/
}

编辑3:

以下是完整演示: https://github.com/nak411/AndroidFilterListDemo