Android AutoCompleteTextView不显示

时间:2019-04-01 06:07:53

标签: android

我正在尝试从Firestore数据库进行autocompletetextview。我已经从Firestore获取数据,并将其放在1个称为autocompleteList的列表中。问题是当我在文本视图中键入内容时,没有出现自动完成功能,但是当我尝试手动添加列表时出现了“ autocompleteList.add(“ 1-Jordan”)。所以我的代码有什么问题?为什么?我的Firestore中的类型值出现了吗?

autocompletelist is filled

这是方法:

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

1 个答案:

答案 0 :(得分:0)

您可以像下面这样创建自定义类

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

}

有关更多建议,请参见Android: AutoCompleteTextView show suggestions when no text entered