0

我正在为涉及数据库的 Android 应用程序制作搜索引擎。我目前的实现如下:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.result_item, R.id.txtVerbName, verbs);
AutoCompleteTextView txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch);
txtSearch.setAdapter(adapter);

但是,您会注意到我已将“txtVerbName”(这是一个 TextView)作为文本视图,它的值随着附加的数组(“动词”)而改变。我怎样才能使这项工作,以便我可以将一个值附加到 txtVerbName ,然后还将一个不同的值附加到同一布局中的另一个 textview?

4

1 回答 1

0

使用自定义适配器来完成此操作:

public class AutoCompleteCursorAdapter extends CursorAdapter implements Filterable {

private TextView txtVerbName, txtVerbDefinition;
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;

public AutoCompleteCursorAdapter(Context context, Cursor c) {
    super(context, c, true);
    mInflater = LayoutInflater.from(context);
    mContext = context;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView txtVerbName = (TextView) view.findViewById(R.id.txtVerbName);
    TextView txtVerbDefinition = (TextView) view.findViewById(R.id.txtVerbDefinition);

    txtVerbName.setText(cursor.getString(1));
    txtVerbDefinition.setText(cursor.getString(2));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = mInflater.inflate(R.layout.result_item, parent, false);
    return view;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    // this is how you query for suggestions
    if (getFilterQueryProvider() != null) {
        return getFilterQueryProvider().runQuery(constraint);
    }
    if (constraint != null) {

        DBAdapter db = new DBAdapter(mContext);
        db.open();

        mCursor = db.getVerbsContaining(constraint);

        mCursor.moveToFirst();
        db.close();
    }
    return mCursor;
}

}

于 2012-05-30T02:18:31.857 回答