我有一个带自定义适配器的ListView,我使用Vector创建TextView 我只想显示包含字母“a”的国家/地区。 我无法找到解决方案。
这是我的适配器以及我是如何尝试的
public class CountryAdapter extends BaseAdapter {
protected MainActivity main = new MainActivity();
private Context mContext;
protected Vector<Country> mVector;
protected SQLiteDatabase mDb;
Cursor cursor;
ImageView deleteIt;
View rowView;
public int pos;
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public CountryAdapter(Context mContext) {
this.mContext = mContext;
mVector = new Vector<Country>();
CountryOpenHelper helper = new CountryOpenHelper(mContext);
mDb = helper.getWritableDatabase();
cursor = mDb.rawQuery("SELECT * FROM COUNTRIES", null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
}
do {
Country country = new Country();
country.setmCountryIndex(cursor.getInt(0));
country.setmCountryName(cursor.getString(2));
country.setmCountryTextSize(cursor.getInt(1));
country.setmCountryColor(cursor.getInt(3));
mVector.add(country);
for (int i = 0; i < cursor.getCount(); i++){
if (mVector.get(i).toString().contains("a") == false){
mVector.remove(i);
}
}
} while (cursor.moveToNext());
如果需要,我的CountryAdapter中的GetView方法也是如此:
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView == null){
tv = new TextView(mContext);
}else{
tv = (TextView) convertView;
}
this.pos = position;
tv.setText(mVector.get(position).getmCountryName());
tv.setTextColor(mVector.get(position).getmCountryColor());
tv.setTextSize(mVector.get(position).getmCountryTextSize());
return tv;
}
答案 0 :(得分:1)
我建议稍微更改路线:使用内置SimpleCursorAdapter
或自定义CursorAdapter
。 Cursors
和CursorAdapters
比将Cursor
转换为Vector
或任何其他类型的List
更有效率和弹性。他们还提供对FilterQueryProvider
类的访问权限,该类将为您筛选Cursor
。
试试这个basic tutorial。