【问题标题】:How do i add search functionality in gridview in android?如何在 android 的 gridview 中添加搜索功能?
【发布时间】:2015-09-12 14:36:02
【问题描述】:

我的 android 应用程序由一个带有图像和其下方文本的 gridview 组成。我将一个字符串数组传递到 textview 中。我希望edittext字段充当搜索栏并过滤结果。我已经看到了其他与此相关的问题,但它们似乎过滤了一个传递给Listview的arraylist。如何在 gridview 中过滤字符串数组?

我知道我应该添加一个 TextWatcher 来监听 EditText 上的文本更改,但之后我该如何过滤我的数据?

这是我的代码

Mainactivity.java

public class MainActivity extends Activity {
EditText inputSearch;
GridView grid;

public static String[] Sname = {
        "one",
        "two",
        "three",
        "four",

};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final CustomGrid adapter = new CustomGrid(MainActivity.this, Sname);
    grid = (GridView) findViewById(R.id.grid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + Sname[+position], Toast.LENGTH_SHORT).show();

        }
    });

    inputSearch = (EditText) findViewById(R.id.editText);


    }
}

CustomGrid.java

public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] Sname;


public CustomGrid(Context c,String[] Sname) {
    mContext = c;

    this.Sname = Sname;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Sname.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

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



@Override
public View getView(int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
        textView.setText(Sname[position]);
        imageView.setImageResource(R.mipmap.image1);

    return grid; }}

【问题讨论】:

    标签: java android arrays gridview filter


    【解决方案1】:

    您可以添加您的适配器,即CustomGrid filter(String) 方法。 您还可以在适配器中添加一个字段 String [] toDisplay

    在您的构造函数中添加 toDisplay = Sname 并在所有其他方法中替换 Snameby toDisplay

    filter 方法中创建一个新的字符串数组,其中仅包含来自Sname 的与所需字符串匹配的字符串。将此新数组设置为toDisplay

    然后在您的CustomGrid 上调用notifyDataSetChanged 方法

    过滤方法示例:

    public void filter(String toMatch) {
         List<String> matches = new ArrayList<String>();
         for (String string : Sname) {
             if (string.matches(toMatch)) {//on whatever mathcing you feel to use
                   matches.add(string);
              }
          }
          toDisplay = new String[matches.size()];
          toDisplay = matches.toArray(toDisplay);
    
         notifyDataSetChanged(); //update your grid view
    }
    

    【讨论】:

    • 能否请您详细说明如何创建过滤方法?
    • 因为数组有定义的大小。
    • 我编辑我的答案,为您提供过滤方法的示例
    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2015-09-16
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多