问题描述
我在android中为对话框创建了单独的布局post.xml。 xml中的自动完成具有以下代码
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="choose a subreddit"
android:id="@+id/subreddit" />.
打开对话框有此代码
public void alertDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.post);
dialog.setTitle("Post");
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View post = inflater.inflate(R.layout.post, null);
AutoCompleteTextView textView = (AutoCompleteTextView)post.findViewById((R.id.subreddit));
String[] subreddits = getResources().getStringArray(R.array.subreddits);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, subreddits);
textView.setAdapter(adapter);
////Autocomplete
//textView.setThreshold(2);
dialog.show();
}
但是对话框内的AutoCompleteTextView未显示自动完成的结果。
1楼
您正在使用以下设置内容视图:
dialog.setContentView(R.layout.post);
然后使用以下方法膨胀不同的视图:
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View post = inflater.inflate(R.layout.post, null);
因此,此View 帖子与您创建的对话框无关。
你必须首先膨胀视图,设置适配器,然后使用dialog.setContentView(post)
final Dialog dialog = new Dialog(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View post = inflater.inflate(R.layout.post, null);
AutoCompleteTextView textView = (AutoCompleteTextView)post.findViewById((R.id.subreddit));
String[] subreddits = getResources().getStringArray(R.array.subreddits);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, subreddits);
textView.setAdapter(adapter);
////Autocomplete
//textView.setThreshold(2);
dialog.setContentView(post);
dialog.setTitle("Post");
dialog.show();