自定义AutoCompleteTextView布局android

时间:2012-10-31 14:11:40

标签: android layout autocompletetextview

我正在尝试创建一个带有AutoCompleteTextView的DialogBox。没关系,我也可以显示自动完成列表和所有内容。问题是我无法对下拉列表进行切割。

这就是我在主要活动中所做的:

DialogBox DB = new DialogBox(MyActivity.this);
DB.createDialog();

而且,在我创建的另一个课程中:

public DialogBox(Context c)
{
    context = c;
}

public void createDialog()
{

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    final View PopupLayout = inflater.inflate(R.layout.dialog_box, null);
    builder.setView(PopupLayout);

    //Autocomplete:
    final AutoCompleteTextView textView = (AutoCompleteTextView) PopupLayout.findViewById(R.id.autocomplete_companies);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.dialog_box, R.id.autocomplete_companies, COMPANIES);
    textView.setAdapter(adapter);
}

如果我尝试更改R.layout.dialog_box,我会收到NullPointerExceptions或其他错误。

编辑:这是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="#FFFFBB33"
    android:contentDescription="@string/app_name"
    android:scaleType="center" />

<AutoCompleteTextView
    android:id="@+id/autocomplete_companies"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:hint="Company Name" />

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:hint="@string/username"
    android:inputType="textEmailAddress" />

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="4dp"
    android:fontFamily="sans-serif"
    android:hint="@string/password"
    android:inputType="textPassword" />

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

我会将适配器子类化。以下是有效的:

    GoSuggestionCursorAdapter adapter = new GoSuggestionCursorAdapter(this, R.layout.autocomplete_line, null, from, to);

和...

public class GoSuggestionCursorAdapter extends SimpleCursorAdapter {

    public static final String GO_SUGGESTION_ID = "_id";
    public static final String GO_SUGGESTION_TITLE = "SUGGESTION_TITLE";
    public static final String GO_SUGGESTION_URL = "SUGGESTION_URL";
    public static final String GO_SUGGESTION_TYPE = "SUGGESTION_TYPE";

    public GoSuggestionCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {      
        super(context, layout, c, from, to);        
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View superView = super.getView(position, convertView, parent);

        ImageView iconView = (ImageView) superView.findViewById(R.id.AutocompleteImageView);

        int resultType;
        try {           
            resultType = Integer.parseInt(getCursor().getString(getCursor().getColumnIndex(GO_SUGGESTION_TYPE)));
        } catch (Exception e) {
            resultType = 0;
        }

        switch (resultType) {
        case 1: iconView.setImageResource(R.drawable.ic_content_purchased); break;
        case 2: iconView.setImageResource(R.drawable.ic_content_blocked); break;
        case 3: iconView.setImageResource(R.drawable.ic_content_expired); break;
        default: break;
        }

        return superView;
    }



}