我正在使用android中的WordList Builder应用程序。该应用程序从数据库中提取单词并显示它们。还有一个“收藏夹”选项卡,仅显示收藏夹。主页面显示所有单词,并在喜欢的单词附近显示一个星号。当用户从“收藏夹”选项卡中取消选择一个单词并移回主页面时,该星标仍会显示在未选择的收藏夹单词上。但是,从数据库中获取的数据清楚地显示了更新。然后我将log语句添加到自定义适配器的getView方法,此处项目列表仍然显示最初加载的单词列表。它没有更新。
以下是自定义适配器的代码:
public class WordListAdapter extends ArrayAdapter<Word> {
protected static final String LOG_TAG = WordListAdapter.class.getSimpleName();
private ArrayList<Word> items;
int layoutResourceId;
private Context context;
public WordListAdapter(Context context,int layoutResourceId,ArrayList <Word> items) {
super(context,layoutResourceId,items);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.i("WordListAdapter" ," NotifyDataSetChanged is called items size = " + this.items.size());
}
public void refreshList(ArrayList<Word> items) {
this.items.clear();
this.items.addAll(items);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
Log.i("WordListAdapter; " , " getView item size =" + this.items.size());
Word wordListItems = this.items.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.wordlist_item, null);
}
TextView tvWordName = (TextView)convertView.findViewById(R.id.wordlist_word);
tvWordName.setText(wordListItems.getWordName());
TextView tvWordId = (TextView)convertView.findViewById(R.id.wordlist_id);
tvWordId.setText(Integer.toString(wordListItems.getId()));
CheckBox cbFav = (CheckBox)convertView.findViewById(R.id.checkBoxFav);
Log.i("WordListAdapter :" , "Position= " + position + " Word " + wordListItems);
cbFav.setChecked(wordListItems.isFavourite());
cbFav.setTag(position);
return convertView;
}
}
以下是主要活动的代码:
public class WordBuilder extends Activity {
//private static SimpleCursorAdapter cursorAdapter = null;
private static WordListAdapter wordListAdapter = null;
private static ListView lv = null;
private static WordListDBSupport dbSupport = null;
TabBar tabbar;
ArrayList<Word> wordList;
Intent intent ;
AdapterView.OnItemClickListener mOnItemClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
TextView tv = (TextView) view.findViewById(R.id.wordlist_id);
int rowId = Integer.parseInt(tv.getText().toString());
Log.i("RowId : ", String.format("%d", rowId));
Bundle bundle = new Bundle();
bundle.putInt("rowId", rowId);
tabbar.getIntent().putExtra("rowId", rowId);
switchTabInActivity(tabbar.WORDLISTDETAILACTIVITY_TAB);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("WordBuilder ", " onCreate() ");
setContentView(R.layout.activity_main);
intent = getIntent();
tabbar = (TabBar) this.getParent();
dbSupport = new WordListDBSupport(this);
lv = (ListView) findViewById(R.id.wordlist);
//If getting tab 3, only the favourites
wordList = new ArrayList<Word>();
wordListAdapter = new WordListAdapter(this,R.id.wordlist, wordList);
lv.setAdapter(wordListAdapter);
lv.setOnItemClickListener(mOnItemClick);
}
public void switchTabInActivity(int indexTabToSwitchTo){
tabbar.switchTab(indexTabToSwitchTo);
}
public void onResume() {
super.onResume();
Log.i("WordBuilder ", " onResume() ");
updateCursorAdapter();
}
public void updateCursorAdapter() {
//Query to get the updated list
wordList.clear();
Log.i("WordBuilder ", " updateCursorAdapter() wordList size = " + wordList.size());
wordList = dbSupport.getAllWords(intent.getIntExtra("isfavourite",0));
Log.i("WordBuilder", "wordlist size after = " +wordList.size() );
wordListAdapter.refreshList(wordList);
}
public void saveFavouritesHandler(View view) {
//To be called on check or uncheck of the checkbox
int position = (Integer)view.getTag();
CheckBox cbFav = (CheckBox)view;
Word wordToUpdate = wordList.get(position);
int rowId = wordToUpdate.getId();
Log.i("RowId isFav: ", String.format("%d ", rowId) + " isfav " + cbFav.isChecked() + " wordname= " + wordToUpdate.getWordName() + " Position = " + position);
if (cbFav.isChecked()) {
//save the selection in the database
dbSupport.updateFavourite(rowId,1);
} else {
dbSupport.updateFavourite(rowId,0);
Log.i("WordBuilder :" , " Position bring removed =" + position );
if(intent.getIntExtra("isfavourite",0) == 1) {
wordList.remove(position);
}
}
updateCursorAdapter();
}
}
我从收藏夹列表中移除了战斗并更新了数据库。 然而,当我回到列出所有单词的主标签时,战斗仍被列为最爱。
答案 0 :(得分:0)
在yiu添加或删除fav图标时,您是否正在更新ListView适配器。如果它们是两个不同的活动,你必须在onResume of mainactivity中更新aapter
答案 1 :(得分:0)
尝试调用adapter.notifyDataSetChanged()。 当你想要刷新列表时。 希望它有所帮助