How to refresh ListAdapter/ListView in android

Posted by user2463990 on Stack Overflow See other posts from Stack Overflow or by user2463990
Published on 2013-10-26T15:50:35Z Indexed on 2013/10/26 15:53 UTC
Read the original article Hit count: 258

Filed under:

I have database with 2 table, custom layout for my listView, and I'm using ListAdapter to display all data on ListView - this works fine. But, I have problem when I wish display something other on listView from my Database. The data is just append on my ListView - I won't this! How I refresh/update ListAdapter?

This is my MainActivity:

ListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                search = (EditText) findViewById(R.id.search);
        lista = (ListView) findViewById(android.R.id.list);
        sqlite = new Sqlite(MainActivity.this);

                //When apps start, listView is populated with data

        adapter = new SimpleAdapter(this, sqlite.getAllData(), 
                    R.layout.lv_layout, 
                    new String[]{"ime","naziv","kolicina","adresa"}, 
                new int[]R.id.kupac,R.id.proizvod,R.id.kolicina,R.id.adresa});

        setListAdapter(adapter);

                search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,int count) {

                        // TODO Auto-generated method stub

                String tekst = s.toString();
            ArrayList<HashMap<String, String>> rez =  
Sqlite.getFilterData(tekst);

         adapter = new SimpleAdapter(MainActivity.this, 
                             rez,R.layout.lv_layout,
            new String[]{"ime","naziv","kolicina","adresa"},
        new int[]{R.id.kupac,R.id.proizvod,R.id.kolicina,R.id.adresa});

            lista.setAdapter(adapter);


            }
    }

The problem is when the method onTextChanged is called. I get all the data but the data just append to my ListView. How to fix it?

And this is my Sqlite class where is needed method:

ArrayList<HashMap<String,String>> results_criteria = new ArrayList<HashMap<String,String>>();
public ArrayList<HashMap<String, String>> getFilterData(String criteria)
    {
        String ime_kupca = "";
        String product = "";
        String adress = "";
        String kolicina = "";

        SQLiteDatabase myDb = this.getReadableDatabase();
        Cursor cursor = myDb.rawQuery("SELECT " + DB_COLUMN_IME + "," + DB_COLUMN_NAZIV + "," + DB_COLUMN_KOLICINA + "," + DB_COLUMN_ADRESA + " FROM " + DB_TABLE1_NAME + "," + DB_TABLE2_NAME + " WHERE kupac.ID = proizvod.ID AND " + DB_COLUMN_IME + " LIKE '%" + criteria + "%'", null);
        if(cursor != null){
            if(cursor.moveToFirst()){
                do{
                    HashMap<String,String>map = new HashMap<String,String>();
                    ime_kupca = cursor.getString(cursor.getColumnIndex(DB_COLUMN_IME));
                    product = cursor.getString(cursor.getColumnIndex(DB_COLUMN_NAZIV));
                    kolicina = cursor.getString(cursor.getColumnIndex(DB_COLUMN_KOLICINA));
                    adress = cursor.getString(cursor.getColumnIndex(DB_COLUMN_ADRESA));
                    map.put("ime", ime_kupca);
                    map.put("naziv", product);
                    map.put("kolicina", kolicina);
                    map.put("adresa", adress);
                    results_criteria.add(map);
                }while(cursor.moveToNext());

                //cursor.close();
            }
            cursor.close();
        }
        Log.i("Rez", "" + results_criteria);
        return results_criteria;

© Stack Overflow or respective owner

Related posts about android