包含多个视图的自定义列表项的Android AutoCompleteTextView

时间:2011-12-15 11:28:10

标签: android sdk

我尝试使用自定义列表项创建AutoCompleteTextView,例如在一个列表项中显示图片和名称。我知道如何使用列表项中的1行文本创建它,但我对使用更多视图执行此操作的人有点困惑。我是关于ListAdapter的事情,并将值分配给正确的视图。我被困在这里了。我希望有人能给我一个正确的方向。 问题在下方更新。

主要活动:

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>

更新

经过大量的硬核谷歌搜索和试错,我想出了这段代码。我认为这很好,但列表项目在选择之后会继续显示。我知道这是打开新列表项的settext。

我发现了这篇文章:Disable Android AutoCompleteTextView after user selects item from drop down

但我不知道他的意思:(任何人都知道如何解决这个问题?

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


}
}

1 个答案:

答案 0 :(得分:3)

使用自定义列表适配器。你可以膨胀布局并分配值

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

`