AutoCompletetextview未显示列表

时间:2014-07-23 08:22:07

标签: android autocompletetextview

我正在使用一个AutoCompleteTextView,当用户键入一个字符时,将显示以该字符开头的单词。但在我的情况下,列表没有显示。

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginRight="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="50dp">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/iv_image"
                    android:layout_gravity="center_horizontal"
                    android:adjustViewBounds="true" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:text="Choose File"
                    android:id="@+id/bt_choose_file"
                    android:layout_gravity="center_horizontal" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/et_Title"
                    android:layout_marginTop="30dp"
                    android:hint="File Name"
                    android:layout_gravity="center_horizontal"
                    style="@style/EditText" />


                <AutoCompleteTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:hint="Document Type"
                    android:imeOptions="actionDone"
                    style="@style/EditText"
                    android:id="@+id/ac_document_type"
                    android:layout_gravity="center_horizontal" />


            </LinearLayout>


            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="30dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Upload"
                    android:textColor="#fff"
                    android:textStyle="bold"
                    android:textSize="25sp"
                    android:id="@+id/bt_upload"
                    android:background="@drawable/button_1"
                    android:layout_marginRight="5dp"
                    android:layout_weight="1" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Cancel"
                    android:textColor="#fff"
                    android:textStyle="bold"
                    android:textSize="25sp"
                    android:id="@+id/bt_cancel"
                    android:background="@drawable/button_1"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

代码

public class UploadDocument extends MasterFragment implements View.OnClickListener {


    private Button btChooseDoc, btUpload, btCancel;
    private EditText etTitle;
    private AutoCompleteTextView acDocumentType;
    private String[] chooseDocumentArray;
    private String userSelection = "";
    ImageView image;
    private Intent intent;
    Bitmap camera, galleryImage;
    Uri galleryUri;
    private String[] documentsArray;
    private ArrayAdapter<String> documentArrayAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.upload_docs_fragment, null, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize();
    }

    private void initialize() {

        btChooseDoc = (Button) getActivity().findViewById(R.id.bt_choose_file);
        acDocumentType = (AutoCompleteTextView) getActivity().findViewById(R.id.ac_document_type);
        btUpload = (Button) getActivity().findViewById(R.id.bt_upload);
        btCancel = (Button) getActivity().findViewById(R.id.bt_cancel);
        image = (ImageView) getActivity().findViewById(R.id.iv_image);
        etTitle = (EditText) getActivity().findViewById(R.id.et_Title);
        btChooseDoc.setOnClickListener(this);
        btUpload.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        setUp();


        switch (view.getId()) {

            case R.id.bt_choose_file:

                displayPopup(getActivity(), "Choose File", chooseDocumentArray, btChooseDoc, false, new GetNamePosition() {
                    @Override
                    public void getName(String name) {
                        userSelection = name;
                        if (userSelection.equals("Gallery")) {
                            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(intent, 0);
                            btChooseDoc.setText("Choose File");
                        }

                        if (userSelection.equals("Camera")) {
                            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(intent, 1);
                            btChooseDoc.setText("Choose File");
                        }

                        if (userSelection.equals("Other")) {
                            intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("file/*");
                            startActivityForResult(intent, 2);
                            btChooseDoc.setText("Choose File");
                        }
                    }

                    @Override
                    public void getPosition(int position) {

                    }
                });
                break;
            case R.id.bt_upload:

                if (isConnected(getActivity())) {

                } else {
                    Crouton.makeText(getActivity(), "Internet Connection not available", Style.ALERT).show();
                }


        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        Bitmap camera;
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {

            camera = (Bitmap) data.getExtras().get("data");
            camera.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
            galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
            image.setImageBitmap(camera);


        }


        if (requestCode == 0 && resultCode == getActivity().RESULT_OK) {

            galleryUri = data.getData();
            try {
                galleryImage = decodeUri(getActivity(), galleryUri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            galleryImage.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
            galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
            image.setImageBitmap(galleryImage);


        }

    }

    private void setUp() {

        chooseDocumentArray = getResources().getStringArray(R.array.Choose_Document);

        // for documents

        documentsArray = getResources().getStringArray(R.array.Document);
        Arrays.sort(documentsArray);
        documentArrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.textview, documentsArray);
        acDocumentType.setAdapter(documentArrayAdapter);


    }

}

我已经正确设置了arrayAdapter,但我仍然没有得到列表

4 个答案:

答案 0 :(得分:0)

您是否实施了这些方法:

@Override
public void afterTextChanged(Editable arg0) {}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}

答案 1 :(得分:0)

尝试

<AutoCompleteTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:hint="Document Type"
                    android:imeOptions="actionDone"
                    style="@style/EditText"
                    android:id="@+id/ac_document_type"
                    android:completionThreshold="1"
                    android:layout_gravity="center_horizontal" />

答案 2 :(得分:0)

在setAdapter()

之后添加此语句

documentArrayAdapter.notifyDataSetChanged();

答案 3 :(得分:0)

获取数组的所有数据后调用autoTextView.setAdapter()而不是调用adapter.notifyDataSetChanged() 如果您的数组项目再次更改,请致电autoTextView.setAdapter()