当前位置: 代码迷 >> Android >> 内部对话框中的autocompletetextview未显示结果
  详细解决方案

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

热度:68   发布时间:2023-08-04 10:14:00.0

我在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未显示自动完成的结果。

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

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();