我有一个ListView,列出了一组书籍。
用户可以添加新书,之后使用onActivityResult方法再次显示书籍集。
我已经尝试了几个小时来刷新新书添加后的书籍集,但是完全没有成功。
这是我尝试过的代码:
public class BookActivity extends Activity {
private ArrayList<Book> books = null;
private BookItemAdapter bookItemAdapter;
ListView booksSetView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
books = new ArrayList<Book>();
booksSetView = (ListView)findViewById(R.id.booksSet);
Cursor booksCursor = null;
booksCursor = getBooksCursor();
if (booksCursor.moveToFirst())
{
do
{
books.add(getBookFromCursor(booksCursor));
} while(booksCursor.moveToNext());
}
bookItemAdapter = new BookItemAdapter(this, R.layout.book_item, books, 0); // BookItemAdapter is a ArrayAdapter
booksSetView.setAdapter(bookItemAdapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.e("BOOKS MANAGER", "trying to refresh");
bookItemAdapter.notifyDataSetChanged();
booksSetView.invalidate();
booksSetView.invalidateViews();
}
}
请帮忙一下。
非常感谢!