Switching Brains
Switching Brains

Reputation: 290

Android AutoCompleteTextView with custom list item containing multiple views

I try to create a AutoCompleteTextView with custom list items, like showing a picture and a name in one list item. I know how to create it with 1 line of text in a list item but i'm a bit confused on who to do this with more views. I was thing about a ListAdapter and assigning the values to the right views. I'm pretty stuck here. I hope someone can give me a push in the right direction. Question is updated below.

Main activity:

public class AutocompleteCustomActivity extends Activity {

String[] firstView = {"Apple","Banana","Strawberry"};
String[] secondView = {"Green","Yellow","Red"};

AutoCompleteTextView autocomplete;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*
    // Simple 1 line list item
    this.autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, firstView);
    autocomplete.setAdapter(adapter);
    */

    // 2 Lines of text in list item
    this.autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.two_list_items, firstView);
    autocomplete.setAdapter(adapter);

}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

 </LinearLayout>

UPDATE:

After a lot of hardcore Googling and trial and erroring i came up with this code. I think it's pretty oké but the list items keep showing after selecting one. I know it's the settext that opens the new listitems.

I found this post: Disable Android AutoCompleteTextView after user selects item from drop down

But i don't know what he means :( Anyone knows how to fix this?

package com.sb.autocompletecustom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;

public class AutocompleteCustomActivity extends Activity {

AutoCompleteTextView autocomplete;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Data to fill autocomplete
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();

    Map<String, String> curGroupMap = new HashMap<String, String>();
    list.add(curGroupMap);
    curGroupMap.put("name", "Banana");
    curGroupMap.put("color", "Yellow");

    curGroupMap = new HashMap<String, String>();
    list.add(curGroupMap);
    curGroupMap.put("name", "Strawberry");
    curGroupMap.put("color", "Red");

    curGroupMap = new HashMap<String, String>();
    list.add(curGroupMap);
    curGroupMap.put("name", "Strawberry");
    curGroupMap.put("color", "Black");


    // 2 Lines of text in list item
    this.autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);

    SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.two_list_items, new String[] { "name", "color" }, new int[] { R.id.textView1, R.id.textView2 });
    autocomplete.setAdapter(adapter);

    autocomplete.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
            Map<String, String> map = (Map<String, String>) p.getItemAtPosition(pos);
            String itemName = map.get("name");
            autocomplete.setText(itemName);
        }
    });


}
}

Upvotes: 0

Views: 8269

Answers (1)

AD14
AD14

Reputation: 1218

use a custom list adapter. you can inflate the layout and assign the values

public class AutoCompleteCursorAdapter extends CursorAdapter implements Filterable{
    private TextView  txtDrName, txtDrugName, txtDrugManufacturer;
    private int rowResID;
    private static Cursor c;
    private String autoCompleteTextName;
    Context context;

int layout;
public AutoCompleteCursorAdapter(Context context, int layout ) {
    super(context, c);
    //    this.c = c;
    this.context = context;
    this.autoCompleteTextName = autoCompleteTextName;
    this.layout = layout;

}
public long getItemId(int position) {  
    return position;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater inflater = LayoutInflater.from(context);

    View v = inflater.inflate(layout, parent, false);


        txtDrName = (TextView)v.findViewById(R.id.txtAutoName) ;
        ....



}

        return v;



    }
@Override
public void bindView(View view, Context context, Cursor cursor) {


        txtDrName = (TextView) view.findViewById(R.id.txtAutoName) ;

}

@Override
public String convertToString(Cursor cursor) {
    // this method dictates what is shown when the user clicks each entry in your autocomplete list
    String name="";

        name = cursor.getString(cursor.getColumnIndex("prefix"))+" "+cursor.getString(cursor.getColumnIndex("firstName"));


    }


    return name;
}

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

        DataBaseHelper db = new DataBaseHelper(context);
        db.openDataBase();
        if(autoCompleteTextName.equals(AppConstants.AUTOCOMPLETEDOCTORNAME)){
            c = db.getStaffStartingWith((String) constraint);

        }
        else if (autoCompleteTextName.equals(AppConstants.AUTOCOMPLETEDRUGNAME)){
            c = db.getDrugsForStartingWith((String) constraint);
        }

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

`

Upvotes: 3

Related Questions