如何实现搜索功能?

时间:2013-11-11 05:38:11

标签: android

我有一个网格视图,我必须在操作栏中添加搜索功能。所以无论我输入什么类型进行搜索,这些项目应该是我搜索到的任何内容。网格视图有图像和文本。我坚持这个很糟糕。我不知道如何实现它。我经历了android指南,但我无法得到它。

请建议一些简单的方法。我需要在watsaap应用程序中搜索。

感谢任何建议。谢谢。

这是我的gridview代码。

public class FragmentDeals extends Fragment implements Checkable{

        private boolean mChecked;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View layout = inflater.inflate(R.layout.fragmentdeal,   
                    (ViewGroup)  
                    findViewById(R.id.layoutdeal));   


            //View view = inflater.inflate(R.layout.griddeal,null);
            final GridView mGrid = (GridView) layout.findViewById(R.id.GridDeal);
            mGrid.setAdapter(new DealAdapter());

            mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);

            mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener() {


                @Override
                public boolean onActionItemClicked(ActionMode mode,
                        MenuItem item) {
                    // TODO Auto-generated method stub
                    return true;
                }

                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.setTitle("Select Items");
                    mode.setSubtitle("One item selected");
                    return true;

                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onItemCheckedStateChanged(ActionMode mode,
                        int position, long id, boolean checked) {

                    //listView.setLongClickable(false);
                    int selectCount = mGrid.getCheckedItemCount();
                    switch (selectCount) {
                    case 1:
                        mode.setSubtitle("One item selected");
                        break;
                    default:
                        mode.setSubtitle("" + selectCount +"items selected");
                        break;

                    }
                }
            });



            return layout;
            // TODO Auto-generated method stub

        }


        @Override
        public boolean isChecked() {
            // TODO Auto-generated method stub
            return mChecked;
        }

        @SuppressWarnings("deprecation")
        @Override
        public void setChecked(boolean checked) {
            // TODO Auto-generated method stub
            mChecked = checked;


            layout.setBackgroundDrawable(checked ? getResources().getDrawable(
                    R.drawable.bground) : null);

        }

        @Override
        public void toggle() {
            // TODO Auto-generated method stub
            setChecked(!mChecked);

        }
    }

    private class DealAdapter extends BaseAdapter {

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

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //CheckableLayout l;

            View myView = convertView;

            LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
            myView = inflater.inflate(R.layout.griddealitems, null);


            // Add The Image!!!           
            ImageView iv = (ImageView)myView.findViewById(R.id.grid_deal_image);
            iv.setImageResource(mThumbIds1[position]);


            // Add The Text!!!
            TextView tv = (TextView)myView.findViewById(R.id.grid_deal_text);
            tv.setText(names1[position] );




            return myView;
        }


        private Integer[] mThumbIds1= {
                R.drawable.car, R.drawable.car,
                R.drawable.car, R.drawable.car,
                R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
                R.drawable.car, R.drawable.car,
                R.drawable.car,R.drawable.car,R.drawable.car
        };

        private String[] names1={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};

    }

1 个答案:

答案 0 :(得分:0)

我从未尝试使用gridview进行搜索功能。但我已经为列表视图做了同样的事情。请查看以下链接以实现listview的搜索功能

使用列表视图搜索功能

http://www.androidbegin.com/tutorial/android-search-listview-using-filter/