在对话框片段中自动完成textview,显示android中的建议

时间:2014-08-29 06:14:13

标签: android autocomplete dialog android-dialogfragment dialogfragment

我有一个像这样的dialogfragment类:

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


        }

    }

在其布局xml文件from.xml中,它包含一个自动完成的textview。这是from.xml文件的代码

<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>

我点击按钮时使用以下代码弹出对话框片段。所以我这样给了:

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


        }
    });

现在我需要创建一个自动完成textview的对象,它应该向我显示建议。我有一个arraylist,其中包含自动完成文本视图的arrayadapter的数据,我编码如下(此处actv1是自动完成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);
            }
        });

但问题是我不知道应该在哪里正确添加此代码,这样当我点击按钮时会弹出一个对话框,其中包含autocompletetextview。在输入时,它应该使用我在这里提到的代码显示建议。有人可以帮帮我......

1 个答案:

答案 0 :(得分:1)

我认为,您需要在对话框自定义视图中找到控件。我没有在代码中看到从视图中找到控件。希望得到帮助。
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();