我正在尝试构建一个包含AutoCompleteTextView的对话框,以提示用户输入信息。虽然大多数一切正常,其中自动完成呈现从数据库加载的正确建议,点击建议没有任何反应,并且文本字段没有填充自动完成建议。单击建议后,光标看起来好像要自己编辑建议,尽管这样做也没有。我在版本4.3设备上运行它,所以我不相信这是问题。
实现警报对话框的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();
}
Dialog自定义视图的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 :(得分: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);