内部对话框中的autocompletetextview未显示结果

时间:2015-10-19 08:36:01

标签: android xml

我在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 个答案:

答案 0 :(得分:3)

您正在使用以下方式设置内容视图:

dialog.setContentView(R.layout.post);

然后使用以下方法对其他视图进行充气:

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View post = inflater.inflate(R.layout.post, null);

因此,此视图帖子与您创建的对话框无关。

您必须首先充气视图,设置适配器然后使用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();