私は2つのテーブル、listViewのカスタムレイアウトを持つデータベースを持っており、ListAdapterを使用してListViewにすべてのデータを表示しています - これはうまくいきます。しかし、データベースから listView に何か他のものを表示したい場合に問題があります。データは私の ListView に追加されるだけです - 私はこれをしません! ListAdapter を更新/更新するにはどうすればよいですか?
これは私の 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);
}
}
問題は、メソッド 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;