简体   繁体   English

Android AutoCompleteTextView不显示

[英]Android AutoCompleteTextView Doesn't show

Im trying to make a autocompletetextview from Firestore database. 我试图从Firestore数据库制作一个autocompletetextview。 I already get the data from Firestore and put it in 1 list called autocompleteList. 我已经从Firestore获取数据并将其放在名为autocompleteList的1个列表中。 The problem is when I type in the textview, the autocomplete doesn't appear but when I try to add list manually "autocompleteList.add("1 - Jordan") it appears. so what is the problem with my code ? why can't my type value from Firestore appear ? 问题是,当我输入textview时,自动完成没有出现但是当我尝试手动添加列表时“autocompleteList.add(”1 - Jordan“)出现了。那么我的代码有什么问题?为什么可以'我的Firestore的类型值出现了吗?

autocompletelist充满了

this is the method: 这是方法:

private void openDialog(){
        LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();

    final View v = li.inflate(R.layout.alertdialog_create_receipt, null);
    final Builder builder = new Builder(CreateReceiptActivity.this);
    builder.setView(v);
    final AutoCompleteTextView addItemType = v.findViewById(R.id.alertdialog_receipt_type);
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("watchlist").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());

                            String type = document.getString("type");
                            autocompleteList = new ArrayList<String>();
                            autocompleteList.add(type);
                        }
                        ArrayAdapter<String> acadapter = new ArrayAdapter<String>(CreateReceiptActivity.this,
                                R.layout.list_autocomplete, R.id.autocomplete_itemtype, autocompleteList);
                        addItemType.setAdapter(acadapter);
                    } else {
                        Log.w(Tag.ITEM, "error getting documents", task.getException());
                    }
                }
            });

    final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
    final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
    Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);
    addItemType.setText(qrResult);
    final AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
            btnScan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
                    startActivityForResult(i, QR_REQUEST_CODE);
                }
            });
        }
    });
    alertDialog.show();
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            itemType = addItemType.getText().toString().trim();
            itemQty = addItemQty.getText().toString().trim();
            itemPrice = addItemPrice.getText().toString().trim();
            listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice, "0");
            receiptItemList.add(listReceiptItem);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            alertDialog.dismiss();
            qrResult = null;
            Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
        }
    });
}

you can make custom class like below 你可以像下面那样制作自定义类

 import android.content.Context;  
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.widget.AutoCompleteTextView;

    public class InstantAutoComplete extends AutoCompleteTextView {

        public InstantAutoComplete(Context context) {
            super(context);
        }

        public InstantAutoComplete(Context arg0, AttributeSet arg1) {
            super(arg0, arg1);
        }

        public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
            super(arg0, arg1, arg2);
        }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused && getAdapter() != null) {
            performFiltering(getText(), 0);
        }
    }

}

For more suggestions refer Android: AutoCompleteTextView show suggestions when no text entered 有关更多建议,请参阅Android:AutoCompleteTextView在未输入任何文本时显示建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM