适配器方法notifyDataSetChanged不刷新listview

时间:2017-02-12 19:00:07

标签: android listview android-arrayadapter

我可以在这个奇怪的问题上使用一些帮助。 首次加载时,我的列表视图为空白。 如果我再次单击以加载页面,退出到另一个活动或刷新相同的活动,我的列表会显示。 有任何想法吗?感谢。

我的变量:

private static ArrayList<Media_Info> mediaIntroQueueList = new ArrayList<Media_Info>();
private static ArrayList<String> mediaIntroQueueListTitles = new ArrayList<>();
ArrayAdapter<String> stringAdapter;
ListView mediaList;

OnCreate:我在哪里设置了stringAdapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_meet);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

    // LOG
    Log.i("Constants", Constants.userID);

    mediaList = (ListView) findViewById(R.id.mediaListView);

    stringAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            mediaIntroQueueListTitles);
    mediaList.setAdapter(stringAdapter);


@Override
protected void onStart() {
    super.onStart();

    Log.i("ACTIVITY", "onStart");

    getUserLocation();

    getLocalIntroductions();

}

private void getLocalIntroductions() {

/// a bunch of firebase db calls

                if (mediaLocationKeysWithinRadius.contains(mediaID)) {

                    Log.i("media within radius: ", mediaID);
                    newItems.add(mediaInfoDic);
                    newItemsTitles.add(title);

                    }
                else {
                    Log.i("media not in radius: ", mediaID);
                }
            }

            mediaIntroQueueList = newItems;
            mediaIntroQueueListTitles = newItemsTitles;

            stringAdapter.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:0)

因为您为mediaIntroQueueListTitles分配了一个新的ArrayList。应该直接使用add()。

private void getLocalIntroductions() {

    /// a bunch of firebase db calls
    // Clear your ArrayLists if you need to
    mediaIntroQueueList.clear();
    mediaIntroQueueListTitles.clear();
    if (mediaLocationKeysWithinRadius.contains(mediaID)) {
        Log.i("media within radius: ", mediaID);
        mediaIntroQueueList.add(mediaInfoDic);
        mediaIntroQueueListTitles.add(title);
    }
    else {
        Log.i("media not in radius: ", mediaID);
    }
    stringAdapter.notifyDataSetChanged();
}