Android的 - 我不能刷新/重绘一个ListViewAndroid、ListView

2023-09-06 05:59:49 作者:花开花败总归尘

我有一个ListView,列出了一套书。结果用户可以添加新的书,并且在这之后,示出了一套书再次使用的onActivityResult方法

I have a ListView that lists a set of books. The user can add a new book and, after that, the set of books is shown again using the onActivityResult method.

我一直在尝试了几个小时,以获得帐套刷新添加新的书后,但没有运气,在所有。

I have been trying for hours to get the set of books to refresh after adding a new book but with no luck, at all.

这是code我曾尝试:

This is the code I have tried:

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

请一些帮助。结果非常感谢!

Please some help. Thanks a lot!

推荐答案

我不知道你在哪里更新书籍的ArrayList是在ListView背后的数据。如果你更新的onActivityResult该对象那么它应该正常工作。

I don't see where you are updating the books ArrayList which is the data behind the ListView. If you update that object in onActivityResult then it should work correctly.