shanky
shanky

Reputation: 801

autocompletetextview inside dialog box not showing results

I have created separate layout post.xml for dialog box in android. Autocomplete inside xml has following code

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="choose a subreddit"
        android:id="@+id/subreddit" />.

Open dialog has this code

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

But AutoCompleteTextView inside dialog box isn't showing autocompleted results.

Upvotes: 1

Views: 1798

Answers (1)

Archie.bpgc
Archie.bpgc

Reputation: 24012

You are setting content view using:

dialog.setContentView(R.layout.post);

and then inflating a different view using:

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

So, this View post is not related to the dialog you created.

You have to inflate the View first, set the adapter and then use 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();

Upvotes: 3

Related Questions