2

I have a dialogfragment class like this:

public class dialogfrag extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater1 = getActivity().getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(inflater1.inflate(R.layout.from, null)) // Add action buttons .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // sign in the user ... } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); return builder.create(); } } 

In its layout xml file from.xml it contains an autocomplete textview in it. This is code of the from.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="50dp" android:text="From" android:gravity="center" android:background="#FFFFBB33" android:textAppearance="?android:attr/textAppearanceLarge" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView1d" android:layout_width="fill_parent" android:layout_marginTop="15dp" android:layout_height="wrap_content" android:hint="From" > <requestFocus /> </AutoCompleteTextView> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Selected Place" android:layout_marginTop="20dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:weightSum="2" > </LinearLayout> 

I'm using the following code to popup the dialog fragment upon button click. So I have given like this:

 Button from=(Button)rootView.findViewById(R.id.button4); from.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub DialogFragment dialog=new dialogfrag(); dialog.show(getFragmentManager(), "fromtag"); } }); 

Now I need to create an object of autocomplete textview and it should show me suggestions. I have an arraylist which contains the data for the arrayadapter of autocomplete text view and I have coded like this (here actv1 is the object of the autocomplete textview):

 actv1.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String filter = s.toString().toLowerCase(); listItems = new ArrayList<String>(); for (String listItem : loc) { if (listItem.toLowerCase().contains(filter)) { listItems.add(listItem); } } if (listItems.size() < 1){ Toast toast = Toast.makeText(getActivity().getApplicationContext(), "No entries contain your search parameters", Toast.LENGTH_SHORT); toast.show(); } ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems); actv1.setAdapter(adapt); // actv1.setAdapter(new ArrayAdapter<String>(getActivity(), // android.R.layout.simple_list_item_1, listItems // .size())); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems); actv1.setAdapter(adapt); } }); 

But the problem is that I donot know where should I add this code properly so that when I click on the button, a dialog pops out and it contains autocompletetextview in it. While typing, it should show suggestions using the code which I have mentioned here. Can some one please help me out...

2
  • You want to ask where to add actv1.addTextChangedListener event? Commented Aug 29, 2014 at 6:21
  • yes...brother...when I add actv1.addtextchangedlistener it is showing null pointer exception Commented Aug 29, 2014 at 6:25

1 Answer 1

2

I think, you need to find control in your dialog custom view. I don't see find the control from view in your code. Hope for help.
In your onCreateDialog

LayoutInflater inflater1 = getActivity().getLayoutInflater(); View view = inflater1 .inflate(R.layout.from, null); AutoCompleteTextView actv1 = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView1d); actv1.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Your code ......... } } builder.setView(view); return builder.create(); 
Sign up to request clarification or add additional context in comments.

3 Comments

One more doubt... How do I send the value in autocomplete textview to my parent fragment..??
you mean dialog fragment controls value send to fragment?
When a user types values in autocomplete, suggestions are shown and user selects one of the suggestions from it. When the ok button of dialog is pressed, I want the suggestion to be received at my main fragment..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.