Mattia Monari
Mattia Monari

Reputation: 168

ArrayAdapter not showing added elements in ListView, Android Studio

I recently added a SearchView element to my menu in my app, which filter correctly the elements in my ListView. The problem comes after i search something, in fact, if i try to add an element to my ListView, it doesn't show the element. The element added actually goes inside the ArrayList in my code, i checked it through Debugging Tools. Any ideas?

This is the OnQueryTextListener for my SearchView:

 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) { 
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) { 
            adapter.getFilter().filter(s);
            return true;
        }
    });

I think the problem is the ArrayAdapter. If you need further details, i can post them here.

Edit #1

ArrayAdapter:

    adapter = new ArrayAdapter<Contatto>(this, android.R.layout.simple_list_item_1, r.arr);
    rubrica.setAdapter(adapter);

Add Element method:

    public void Aggiungi(View view){

    String n,c,t;

    n = editNome.getText().toString();

    c = editCognome.getText().toString();

    t = editNumero.getText().toString();

    if(!PhoneNumberUtils.isGlobalPhoneNumber("+39" +  t) || (t.length() < 8) ){
        Toast.makeText(this, "Inserisci un numero internazionale valido.", Toast.LENGTH_SHORT).show();
        return;
    }

    r.aggiungiContatto(n , c , t); //C'è qualcosa che non va nel filtrare i tutto.
    adapter.notifyDataSetChanged();
}

r is the object that contains the ArrayList. The "aggiungiContatto" method is:

public void aggiungiContatto(String nome, String cognome, String numeroCell){
    Contatto c=new Contatto(nome, cognome, numeroCell);
    arr.add(c);
}

Should be enough.

Upvotes: 0

Views: 578

Answers (1)

i_A_mok
i_A_mok

Reputation: 2904

Below may be a work around, try:

        @Override
        public boolean onQueryTextChange(String s) {
            adapter.getFilter().filter(s);
            if (s.equals("")) {
                adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, r.arr);
                rubrica.setAdapter(adapter);
            }
            return true;
        }

Upvotes: 1

Related Questions