Android 在ListView中显示包含特定字母的视图
我有一个带有自定义适配器的ListView,我使用Vector创建TextView 例如,我只想显示包含字母“a”的国家。 我找不到解决办法 这是我的适配器和我如何尝试这样做Android 在ListView中显示包含特定字母的视图,android,string,listview,string-comparison,Android,String,Listview,String Comparison,我有一个带有自定义适配器的ListView,我使用Vector创建TextView 例如,我只想显示包含字母“a”的国家。 我找不到解决办法 这是我的适配器和我如何尝试这样做 public class CountryAdapter extends BaseAdapter { protected MainActivity main = new MainActivity(); private Context mContext; protected Vector<Country> mVec
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());
我建议稍微改变一下方向:使用内置的
SimpleCursorAdapter
或自定义CursorAdapter
<代码>光标和光标适配器
比将光标
转换为向量
或任何其他类型的列表
更高效、更具弹性。它们还提供对过滤器查询provider
类的访问,该类将为您过滤光标
试试这个。在for循环中,我将其更改为mVector.size();还是什么也没发生。。
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;
}