0

我有 2 个表的数据库,我的 listView 的自定义布局,并且我正在使用 ListAdapter 在 ListView 上显示所有数据 - 这工作正常。但是,当我希望在我的数据库中的 listView 上显示其他内容时,我遇到了问题。数据只是附加在我的 ListView 上 - 我不会这样!如何刷新/更新 ListAdapter?

这是我的主要活动:

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


            }
    }

问题是当调用 onTextChanged 方法时。我得到了所有数据,但数据只是附加到我的 ListView。如何解决?

这是我的 Sqlite 类,需要方法:

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;
4

0 回答 0