我有一个带有自定义适配器的列表视图,该适配器具有两个 TextView 和一个图像。我正在使用 ActionBarSherlock 并想添加一个搜索功能,以便用户可以使用操作栏中的操作搜索两个 TextView 中的任何一个。问题是,我完全不知道如何做到这一点,因为我是初学者。
这是显示列表视图的类:
public class ItemId extends SherlockListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplication();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.item_ids);
String[] ids = res.getStringArray(R.array.item_names);
TypedArray icons = res.obtainTypedArray(R.array.item_images);
setListAdapter(new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons));
}
这是列表视图的适配器类:
public class ItemIDAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private String[] mIds;
private TypedArray mIcons;
private int mViewResourceId;
public ItemIDAdapter(Context ctx, int viewResourceId,
String[] strings, String[] ids, TypedArray icons) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIds = ids;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
TextView tv1 = (TextView)convertView.findViewById(R.id.itemids);
tv1.setText(mIds[position]);
return convertView;
}
}
}