0

I am making a search engine for my Android app that involves a database. My current implementation is as follows:

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);

However, you will notice I have put "txtVerbName" (which is a TextView) as the textview that gets its value changed with the array attached ("verbs"). How can I make this work so that I can attach a value to txtVerbName and then also attach a different value to another textview in the same layout?

Qasim
  • 1,686
  • 4
  • 27
  • 51
  • You will needs to implement your own extension of `ArrayAdapter`. That way you have precise control over what data you bind up with which views. There are literally tons of examples to be found here on SO and your favourite search engine. – MH. May 30 '12 at 02:03
  • @MH. I am sorry for posting this question, but when I found those examples (before posting this question) they were confusing me and I didn't quite understand them. I took the time to read over what was actually happening and implemented my own custom AutoCompleteAdapter and succeeded in doing what I wanted. – Qasim May 30 '12 at 02:17

1 Answers1

0

Use a Custom Adapter to accomplish this:

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;
}

}

Qasim
  • 1,686
  • 4
  • 27
  • 51