2013-11-27 63 views
0

我想構建一個對話框,其中包含一個AutoCompleteTextView提示用戶輸入信息。雖然大多數情況都可行,其中自動完成提示從數據庫加載的正確建議,但點擊建議後不會發生任何事情,並且文本字段不會填充自動填充建議。點擊這些建議後,光標看起來就好像自己編輯建議一樣,儘管它也不做任何事。我在4.3版本的設備上運行它,所以我不認爲這是問題所在。Android:AutoCompleteTextView的建議不可點擊裏面的對話框

實施警告對話框中的Java代碼是在這裏:

public class MainActivity extends Activity { 
public static final String PREFS_NAME = "ChosenGroupName"; 
public final static String EXTRA_MESSAGE = "com.example.tperm.MESSAGE"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    final SharedPreferences settings = getSharedPreferences(PREFS_NAME,0); 
    final String previousGroupName = settings.getString("groupName", "No Group Specified"); 
    final SharedPreferences.Editor prefseditor = settings.edit(); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final SamplesDataSource mSamplesDataSource = new SamplesDataSource(
      this); 
    mSamplesDataSource.open(); 
    mSamplesDataSource.addGroupNameifUnique("No Group Specified"); 
    List<String> groupNames = mSamplesDataSource.getAllGroupNames(true); 
    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.group_name_dialog); 
    dialog.setTitle("Please Enter a Group Name for the Samples"); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(builder.getContext(),R.layout.group_name_dialog,R.id.group_name_autocomplete,groupNames); 

    LayoutInflater inflater = this.getLayoutInflater(); 
    View v = inflater.inflate(R.layout.group_name_dialog,null); 

    final AutoCompleteTextView groupNameAutocomplete = (AutoCompleteTextView) v.findViewById(R.id.group_name_autocomplete); 
    groupNameAutocomplete.setAdapter(adapter); 
    groupNameAutocomplete.setThreshold(1); 
    groupNameAutocomplete.setPadding(0, 100, 100, 0); 


    builder.setView(v) 
    .setNegativeButton("Enter", new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface dialog, int id){ 
      if(groupNameAutocomplete.getText()!=null){ 
       String chosenGroupName = groupNameAutocomplete.getText().toString(); 
       prefseditor.putString("groupName", chosenGroupName); 
       prefseditor.commit(); 
       mSamplesDataSource.addGroupNameifUnique(chosenGroupName); 
       dialog.dismiss(); 
       Toast.makeText(getApplicationContext(), "Group Name is: "+ chosenGroupName + 
         ". You may change this by going into the settings menu.",Toast.LENGTH_LONG).show(); 
      } 
      else{ 
       Toast.makeText(getApplicationContext(), "No group name selected, you may use the last group name used for this system" + 
        " by selecting the \"use last group name\" button",Toast.LENGTH_LONG).show(); 
      } 
     } 
    }) 
    .setPositiveButton("Use Last Group name", new DialogInterface.OnClickListener() { 
    @Override 
     public void onClick(DialogInterface dialog, int id) { 

       Toast.makeText(getApplicationContext(), "Group Name is: "+ previousGroupName + 
         ". You may change this by going into the settings menu.",Toast.LENGTH_LONG).show(); 
      dialog.dismiss();// We will be getting the group name string from the prefs 
      //and we already loaded the last group name. 
     } 
}); 
    builder.show(); 

} 

該對話框的自定義視圖中的XML代碼如下:

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

    <AutoCompleteTextView 
     android:id="@+id/group_name_autocomplete" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" 
     android:hint="Group Name" > 

    </AutoCompleteTextView> 

</RelativeLayout> 

編輯:我想通了,我的問題。它在實例化ArrayAdapter的行中用於自動填充建議。在資源參數中,您可以看到我指定了AutoCompleteTextView本身,而不是ndroid.R.layout.simple_dropdown_item_1line,因爲它應該是。

正確的路線將如下:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(builder.getContext(),android.R.layout.simple_dropdown_item_1line,R.id.group_name_autocomplete,groupNames); 

回答

0

編輯:我想通了,我的問題。它在實例化ArrayAdapter的行中用於自動填充建議。在資源參數中,您可以看到我指定了AutoCompleteTextView本身,而不是android.R.layout.simple_dropdown_item_1line,因爲它應該是。

正確的路線將如下:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(builder.getContext(),android.R.layout.simple_dropdown_item_1line,R.id.group_name_autocomplete,groupNames);