搜索自定义列表视图

时间:2013-08-20 02:31:59

标签: android listview search adapter textwatcher

我很好奇如何使用可搜索的自定义适配器制作列表视图。我按照教程进行了设置,但是当我尝试在EditText中输入任何内容时,应用程序崩溃了。现在我知道应用程序崩溃的原因,我只是不知道如何解决它。

以下是我的MainActivity的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Context ctx = getApplication();
        Resources res = ctx.getResources();

        String[] options = res.getStringArray(R.array.item_ids);
        String[] ids = res.getStringArray(R.array.item_names);
        TypedArray icons = res.obtainTypedArray(R.array.item_images);

        setListAdapter(new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons));
    }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Get the options menu view from menu.xml in menu folder
            getSupportMenuInflater().inflate(R.menu.items_menu, menu);

            // Locate the EditText in menu.xml
            editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();

            // Capture Text in EditText
            editsearch.addTextChangedListener(textWatcher);

            // Show the search menu item in menu.xml
            MenuItem menuSearch = menu.findItem(R.id.menu_search);

            menuSearch.setOnActionExpandListener(new OnActionExpandListener() {

                // Menu Action Collapse
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // Empty EditText to remove text filtering
                    editsearch.setText("");
                    editsearch.clearFocus();
                    return true;
                }

                // Menu Action Expand
                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    // Focus on EditText
                    editsearch.requestFocus();

                    // Force the keyboard to show on EditText focus
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                    return true;
                }
            });

            // Show the settings menu item in menu.xml
            MenuItem menuSettings = menu.findItem(R.id.home);

            // Capture menu item clicks
            menuSettings.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    Intent intent2 = new Intent(ItemId.this, Home.class);
                    startActivity(intent2);
                    return true;
                }

            });

            return true;
        }

        // EditText TextWatcher
        private TextWatcher textWatcher = new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString()
                        .toLowerCase(Locale.getDefault());
                adapter.getFilter().filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

        };

以下是我的自定义列表视图适配器的代码:

public class ItemIDAdapter extends ArrayAdapter<String> {

    public LayoutInflater mInflater;

    public String[] mStrings;
    public String[] mIds;
    public TypedArray mIcons;   
    public int mViewResourceId;
    ArrayAdapter<String> adapter;

    public ItemIDAdapter(Context ctx, int viewResourceId,
            String[] strings, String[] ids, TypedArray icons) {
        super(ctx, viewResourceId, strings);

        mInflater = (LayoutInflater)ctx.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        mStrings = strings;
        mIds = ids;
        mIcons = icons;       
        mViewResourceId = viewResourceId;
    }

    @Override
    public int getCount() {
        return mStrings.length;
    }

    @Override
    public String getItem(int position) {
        return mStrings[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

        ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
        iv.setImageDrawable(mIcons.getDrawable(position));

        TextView tv = (TextView)convertView.findViewById(R.id.option_text);
        tv.setText(mStrings[position]);

        TextView tv1 = (TextView)convertView.findViewById(R.id.itemids);
        tv1.setText(mIds[position]);

        return convertView;
    }

}

当我尝试搜索时应用崩溃的原因是因为在TextWatcher中,这一行:adapter.getFilter().filter(text);适配器仅在我的主要活动的顶部定义,但它不用于我的listview at所有。我被困在这里并且真的不知道用adapter代替什么,因为ItemIDAdapter不起作用。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

替换

setListAdapter(new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons));

adapter = new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons)

setListAdapter(adapter );