2014-07-23 50 views
0

我使用了一個AutoCompleteTextView,其中當用戶鍵入一個字符時,以該字符開頭的單詞將顯示出來。但在我的情況下,列表不顯示。自動完成textview不顯示列表

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,但我仍沒有得到列表

+0

發佈完整的代碼。 – Aniruddha

+0

我已經發布了完整的代碼,plz do chaeck it – user3844417

回答

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" /> 
+0

仍然無法正常工作 – user3844417

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) {} 
0

,一旦你得到的所有數據陣列,而不是調用adapter.notifyDataSetChanged() 如果陣列項目再次改變的添加本聲明setAdapter()後

documentArrayAdapter.notifyDataSetChanged();

0

呼叫autoTextView.setAdapter(),調用autoTextView.setAdapter()