I have a ListView with custom Adapter and I create the TextView using Vector
I want to show only the country that contains the letter “a” for example.
and I can’t find the solution to do this.
Here is my adapter and how I tried to do it
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());
And if required here is my GetView method thats in my CountryAdapter too :
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;
}
I recommend changing directions a bit: use the built-in
SimpleCursorAdapteror a customCursorAdapter.CursorsandCursorAdaptersare more efficient and resilient than converting aCursorinto aVectoror any other type ofList. They also provider access to theFilterQueryProviderclass which will filter theCursorfor you.Try out this basic tutorial.