实现搜索android网格视图

时间:2016-07-19 08:50:07

标签: android android-gridview android-search

我在我的Android应用程序中有gridview,它显示图像和文本,所以我想在gridview中添加searchview,这样当我在searchview中输入内容时,我应该能够看到匹配的gridview items.i不知道如何实现我的搜索视图用于链接到该网格的数组。 提前致谢

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

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

public long getItemId(int position) {
    return 0;
}

@Override



// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;


    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder mVHolder;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        convertView = inflater.inflate(R.layout.grid_single, parent, false);
        mVHolder=new ViewHolder();



        mVHolder.mTextView=(TextView)convertView.findViewById(R.id.text);
        convertView.setTag(mVHolder);

    } else {
        mVHolder=(ViewHolder)convertView.getTag();
    }
    mVHolder.mTextView.setText(mThumbIds[position]);

    return convertView;}




// array of strings

private String[] mThumbIds = {......};
 }

class ViewHolder {

    TextView mTextView;

}

1 个答案:

答案 0 :(得分:0)

检查此tutorial有关如何为ListViews提供搜索功能的信息。

您必须创建SearchView

SearchManager searchManager = (SearchManager)
                            getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.search);
    searchView = (SearchView) searchMenuItem.getActionView();

实施SearchView.OnQueryTextListener并添加其未实现的方法

@Override

public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    friendListAdapter.getFilter().filter(newText);

    return true;
}

为您的BaseAdapter实施Filterable。 您必须为Filtered数据创建一个arraylist。