2012-10-31 233 views
1

我想創建一個帶有AutoCompleteTextView的DialogBox。沒關係,我也可以顯示自動完成列表和所有內容。問題是我不能削減對下拉列表的看法。自定義AutoCompleteTextView佈局android

這是我在做什麼在主要活動:

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

任何想法,我做錯了?

+0

你能後的XML的內容,你所提到的錯誤的痕跡? –

+0

新增...謝謝 – pacavied

回答

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



} 
+0

不適用於我的情況:( – pacavied