notifydatasetchanged() doesn't work for autocomplete textview with firebase db adapter

36 Views Asked by At

I have an autocomplete textview in a main activity, I want to aliment my autocomplete textview from a firebase database, as the retrieve of data from firebase db is asynchronized with the main thread so I need to use notifydatasetchanged method with my custom adapter here is the code of my mainactivity and my custom adapter

My main activity



    private ArrayList<Client> clients = new ArrayList<>();
    private int birthdayNumber = 0;
    private AutoCompleteTextView autoCompleteTxv;
    private AutoCompleteTxvAdapter autoCompleteTxvAdapter;
    private ImageButton imageButton;
    private ImageButton imageButtonList;
    private ImageButton imageButtonUnpaid;
    private BadgeView badgeView;
    private ConnexionDB db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar choiceToolbar = findViewById(R.id.mainToolbar);
        setSupportActionBar(choiceToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        initViews();
        setAlarm();
        autoCompleteTxvAdapter = new AutoCompleteTxvAdapter(this, clients);
        autoCompleteTxv.setAdapter(autoCompleteTxvAdapter);
        
        db = new ConnexionDB(this);
        ArrayList<String> keys = new ArrayList<>();
        ConnexionDB.getReference().child("Clients").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Client client = dataSnapshot.getValue(Client.class);
                if (client.getId().isEmpty()){
                    client.setID(dataSnapshot.getKey());
                    db.modifyClientId(client);
                }
                clients.add(client);
                Log.e("TAG", "test client size : "+ clients.size());
                autoCompleteTxvAdapter.notifyDataSetChanged();
                Calendar calendar = Calendar.getInstance();
                String mFormat = "dd/MM/yyyy";
                SimpleDateFormat sdf = new SimpleDateFormat(mFormat);
                String date = sdf.format(calendar.getTime());
                String[] dateSplit = date.split("/");
                String[] ddnSplit = client.getDDN().split("/");
                if (ddnSplit[0].equals(dateSplit[0]) && ddnSplit[1].equals(dateSplit[1])){
                    birthdayNumber += 1;
                    badgeView.setBadgeCount(birthdayNumber);
                }
                keys.add(dataSnapshot.getKey());
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                Client client = dataSnapshot.getValue(Client.class);
                String key = dataSnapshot.getKey();
                int index = keys.indexOf(key);
                clients.add(index, client);
                autoCompleteTxvAdapter.notifyDataSetChanged();
                Calendar calendar = Calendar.getInstance();
                String mFormat = "dd/MM/yyyy";
                SimpleDateFormat sdf = new SimpleDateFormat(mFormat);
                String date = sdf.format(calendar.getTime());
                String[] dateSplit = date.split("/");
                String[] ddnSplit = client.getDDN().split("/");
                if (ddnSplit[0].equals(dateSplit[0]) && ddnSplit[1].equals(dateSplit[1])){
                   birthdayNumber += 1;
                   badgeView.setBadgeCount(birthdayNumber);
                }
                keys.add(index, dataSnapshot.getKey());
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }

        });

    }

    void initViews(){
        autoCompleteTxv = findViewById(R.id.autoComplete_txv);
        imageButton = findViewById(R.id.notification_button);
        imageButtonList = findViewById(R.id.list_button);
        imageButtonUnpaid = findViewById(R.id.unpaid_button);
    }
    
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public void onClick(View view) {
        Intent intent = new Intent(this, AddClientActivity.class);
        this.startActivity(intent);
    }

}

My adapter

    private List<Client> clientsListFull;
    private ConnexionDB db;

    public AutoCompleteTxvAdapter(@NonNull Context context, @NonNull List<Client> clientsList) {
        super(context, 0, clientsList);
        clientsListFull = new ArrayList<>(clientsList);
        db = new ConnexionDB(context);
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.autocomplete_txv_row, parent, false
            );
        }

        TextView mNameTxv = convertView.findViewById(R.id.client_complete_name);
        TextView mIdTxv = convertView.findViewById(R.id.client_id);

        Client client = getItem(position);

        if (client != null) {
            mNameTxv.setText(client.getNom() + " "+ client.getPrenom());
            mIdTxv.setText(String.valueOf(client.getId()));
        }

        return convertView;
    }

    private Filter nameFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            List<Client> suggestions = new ArrayList<>();
            if (constraint == null || constraint.length() == 0) {
                suggestions.addAll(clientsListFull);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();
                for (Client client : clientsListFull){
                        if (client.getPrenom().toLowerCase().contains(filterPattern) || client.getNom().toLowerCase().contains(filterPattern)) {
                            suggestions.add(client);
                            results.values = suggestions;
                            results.count = suggestions.size();
                        }
                }
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            try {
                if (results != null) {
                    clear();
                    addAll((List) results.values);
                    notifyDataSetChanged();
                }
            } catch (Exception e) {
            }
        }

        @Override
        public CharSequence convertResultToString(Object resultValue) {
            return ((Client) resultValue).getNom() + " " + ((Client) resultValue).getPrenom();
        }
    };

}

but the autocomplete text doesn't show any results

When I replace the line code

autoCompleteTxvAdapter.notifyDataSetChanged();

with

autoCompleteTxvAdapter = new AutoCompleteTxvAdapter(MainActivity.this, clients);
autoCompleteTxv.setAdapter(autoCompleteTxvAdapter);

It works fine

Any solutions please ?

0

There are 0 best solutions below