0

我有一个带有自定义适配器的 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;




}
4

1 回答 1

1

我建议稍微改变方向:使用内置SimpleCursorAdapter或自定义CursorAdapter. Cursors并且CursorAdapters比将 a 转换Cursor为 aVector或任何其他类型的List. 他们还提供对将为您FilterQueryProvider过滤的类的访问权限Cursor

试试这个基本教程

于 2013-01-16T18:58:24.863 回答