0

Im trying to make a autocompletetextview from Firestore database. I already get the data from Firestore and put it in 1 list called autocompleteList. 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 ?

autocompletelist is filled

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();
        }
    });
}
  • "String type" are you getting some value – Bharat Kumar Apr 01 '19 at 06:20
  • @BharatKumar yes, I'm getting the "type" value from Firestore DB and then store it to autocompleteList. – Justin jant Apr 01 '19 at 06:21
  • ok...pleas try moving autocompleteList = new ArrayList(); outside the listener..i mean before the listner....edit: best would be before the for loop – Bharat Kumar Apr 01 '19 at 06:22
  • @BharatKumar I make string type into global and move this all outside the listener but still no autocomplete shown . – Justin jant Apr 01 '19 at 06:28
  • String type = document.getString("type"); autocompleteList = new ArrayList(); autocompleteList.add(type); } ArrayAdapter acadapter = new ArrayAdapter(CreateReceiptActivity.this, R.layout.list_autocomplete, R.id.autocomplete_itemtype, autocompleteList); addItemType.setAdapter(acadapter); – Justin jant Apr 01 '19 at 06:28

1 Answers1

0

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

Richa Shah
  • 930
  • 3
  • 12
  • 27