1

我正在尝试创建一个带有 AutoCompleteTextView 的对话框。没关系,我也可以显示自动完成列表和所有内容。问题是我无法自定义下拉列表的视图。

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

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" />

知道我做错了什么吗?

4

1 回答 1

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



}
于 2012-10-31T14:54:27.743 回答