我有一個小問題,與設置的新Adapter
到listView
當用戶在EditText
輸入字母。所以這是我正在做的事情是,當我開始Activity
是第一次,我從填充用光標和自定義適配器數據庫列表視圖。當用戶在EditText中輸入一些文本時,我正在創建新的sql語句並使用新的Cursor獲取數據。之後,我創建了新的自定義適配器並嘗試將其設置爲我的列表視圖。的Android設置新的光標到ListView
問題是,當我開始在編輯文本中輸入時,我可以在LogCat的Logs中看到sql語句是正確的,並且遊標大小,但是我的ListView沒有填充新數據。它保持不變。下面是我如何做它:
這是我如何填充首次ListView控件:
cursor = userDbHelper.executeSQLQuery(sqlQuery);
if (cursor.getCount() == 0) {
noCards.setVisibility(View.VISIBLE);
noCards.setText(getString(R.string.no_cards));
} else if (cursor.getCount() > 0) {
noCards.setVisibility(View.GONE);
for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
cardId.add(objectId);
title = cursor.getString(cursor.getColumnIndex("title"));
catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
count.add(repeats);
if(extra != 0){
String cardsm = "SELECT objectId FROM cardmedias "
+ "WHERE cardId="
+ objectId
+ " AND mediaType="
+ 5012;
Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
if (cardsM.getCount() == 0) {
} else if (cardsM.getCount() > 0) {
for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
objectID = Integer.parseInt(cardsM.getString(cardsM.getColumnIndex("objectId")));
String filename = "mediacard-"+objectID;
if(storageID==1){
path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, this);
} else if(storageID==2){
path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
}
hm.put(objectID, path);
path = hm.get(objectID);
Log.i("","path : "+path);
paths.add(path);
names.add(title);
categories.add(catTitle);
}
}
} else if (extra==0){
names.add(title);
categories.add(catTitle);
}
}
}
cursor.close();
adapter = new LazyAdapter(this, paths, names, categories, count);
listView.setAdapter(adapter);
,這是我如何創建新的光標和適配器TextWatcher
:
searchString = "";
sqlQuery = getSqlQuery(sort, collId, ascDesc,searchString);
searchBar.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
check = 1;
listView.setAdapter(null);
searchString = s.toString();
Log.e("","searchString : "+searchString);
sqlQuery = getSqlQuery(sort, collId, ascDesc,searchString);
Log.e(""," sqlquery : "+sqlQuery);
Cursor cursor = userDbHelper.executeSQLQuery(sqlQuery);
Log.e("","cursor count : "+cursor.getCount());
if (cursor.getCount() == 0) {
noCards.setVisibility(View.VISIBLE);
noCards.setText(getString(R.string.no_cards));
} else if (cursor.getCount() > 0) {
noCards.setVisibility(View.GONE);
for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
cardId.add(objectId);
title = cursor.getString(cursor.getColumnIndex("title"));
catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
count.add(repeats);
if(extra != 0){
String cardsm = "SELECT objectId FROM cardmedias "
+ "WHERE cardId="
+ objectId
+ " AND mediaType="
+ 5012;
Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
if (cardsM.getCount() == 0) {
} else if (cardsM.getCount() > 0) {
for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
objectID = Integer.parseInt(cardsM.getString(cardsM.getColumnIndex("objectId")));
String filename = "mediacard-"+objectID;
if(storageID==1){
path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, OwnedStampii.this);
} else if(storageID==2){
path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
}
hm.put(objectID, path);
path = hm.get(objectID);
Log.i("","path : "+path);
paths.add(path);
names.add(title);
categories.add(catTitle);
}
}
} else if (extra==0){
names.add(title);
categories.add(catTitle);
}
}
}
cursor.close();
LazyAdapter adapter = new LazyAdapter(OwnedStampii.this, paths, names, categories, count);
listView.setAdapter(adapter);
}
});
所以任何想法如何刷新我的列表查看與新的適配器。我已經嘗試過使用adapter.notifySetDataChanged();
,但它不工作。
在此先感謝!