[英]Autocomplete textview in dialog fragment showing suggestions in android
I have a dialogfragment class like this: 我有一个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();
}
}
In its layout xml file from.xml it contains an autocomplete textview in it. 在其布局xml文件from.xml中,它包含一个自动完成的textview。 This is code of the from.xml file
这是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>
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. 现在,我需要创建一个自动完成的textview对象,它应该向我显示建议。 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):
我有一个arraylist,其中包含自动完成文本视图的arrayadapter的数据,并且我已经进行了如下编码(此处actv1是自动完成文本视图的对象):
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. 但是问题是我不知道应该在哪里正确添加此代码,以便在单击按钮时弹出一个对话框,其中包含autocompletetextview。 While typing, it should show suggestions using the code which I have mentioned here.
键入时,它应该使用我在这里提到的代码显示建议。 Can some one please help me out...
有人可以帮帮我吗...
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 在您的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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.