在网格视图中搜索功能,我有自定义适配器,如何在android

时间:2016-05-21 18:53:13

标签: android android-gridview baseadapter

DrawerAdapter.java

public class DrawerAdapter extends BaseAdapter {

Context mContext;

MainActivity.Pack[] pacsForAdapter;


public DrawerAdapter(Context c, MainActivity.Pack lists[]) {
    mContext = c;
    pacsForAdapter = lists;
}

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

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

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

static class ViewHolder{
    TextView text;
    ImageView icon;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    LayoutInflater li= (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null){
        convertView = li.inflate(R.layout.items, null); //items.xml contains a TextView and an ImageView
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView)convertView.findViewById(R.id.icon_text);
        viewHolder.icon = (ImageView)convertView.findViewById(R.id.icon_image);
        convertView.setTag(viewHolder);
    }
    else
        viewHolder = (ViewHolder)convertView.getTag();

    viewHolder.text.setText(pacsForAdapter[position].label);
    viewHolder.icon.setImageDrawable(pacsForAdapter[position].icon);

    return convertView;

    }
}

如何添加搜索功能,我创建了一个EditText mytext =(EditText)findViewById(R.id.searchfield);

我已经搜索了答案,但是,它们似乎有点令人困惑,因为我使用了一个类似于

的课程
class Pack {
    Drawable icon;
    String label;
    String name;
}

此类位于MainActivity.java

来自MainActivity.java的东西

public void set_pacs() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pacsList = packageManager.queryIntentActivities(mainIntent, 0);
lists = new Pack[pacsList.size()];
for (int i = 0; i < pacsList.size(); i++) {
lists[i] = new Pack();
lists[i].icon = pacsList.get(i).loadIcon(packageManager);
lists[i].name = pacsList.get(i).activityInfo.packageName;
lists[i].label = pacsList.get(i).loadLabel(packageManager).toString();
}
new SortApps().exchange_sort(lists);
drawerAdapter = new DrawerAdapter(this, lists);
mylist.setAdapter(drawerAdapter);
mylist.setOnItemClickListener(new DrawerClickListener(this, lists, packageManager));  //DrawerClickListener I'm using this to listen to clicks, i think it's another thing, not related to search.
}

我已经研究过一些关于 getFilter 的内容,但很困惑,这与文本有何关联,或如何根据标签过滤项目

我已经花了两天时间,寻找解决方案,但是:(

请帮助

感谢。

0 个答案:

没有答案