2013-11-27 28 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);