1

我有一个网格视图,我必须在操作栏中添加搜索功能。所以无论我输入什么搜索,项目都应该来自我搜索的任何内容。网格视图有图像和文本。我非常糟糕地陷入了这个状态。我不知道如何实现它。我浏览了 android 指南,但我无法得到它。

请提出一些简单的方法。我需要像在 watsaap 应用程序中那样进行搜索。

任何建议表示赞赏。谢谢。

这是我的网格视图代码。

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","","","","","","",""};

    }
4

1 回答 1

0

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

具有列表视图的搜索功能

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

于 2013-11-11T05:46:00.910 回答