无变化/黑屏由notifyDataSetChanged();黑屏、无变化、notifyDataSetChanged

2023-09-04 05:09:45 作者:低调成本性i

我想要做这样的事情。

而pressing中的刷新的按钮,它会刷新列表。活动codeS是这样的:

while pressing the refresh button, it will refresh the list. Activity codes are like this:

        adapter = new TweetAdapter(Welcome.this, tweets, users);
        tweetsList.setAdapter(adapter);


    private void refreshAdapter() {
        adapter.clear();
        adapter.refresh(tweets, users);
    }

refreshAdapter()调用的时候,我需要改变的列表项。这里鸣叫&放大器; 用户的ArrayList &放大器; 的HashMap 项目

refreshAdapter() called when I need to change the list item. here tweets & users are ArrayList & HashMap items

和适配器的部分是这样的:

and the adapter part is like this:

public void clear() {
    this.tweets.clear();
    this.users.clear();
}

public void refresh(ArrayList<Tweet> tweets, HashMap<String, User> users) {
    this.tweets.addAll(tweets);
    this.users.putAll(users);
    Collections.sort(this.tweets, new TweetTimeComparator());
    notifyDataSetChanged();
}

但遗憾的是 notifyDataSetChanged(); 不工作。通过点击刷新的名单要么变成黑色(最长时间)或无响应(一段时间)。

but unfortunately notifyDataSetChanged(); is not working. by clicking refresh the list either turns black(maximum time) or have no response(some time).

请告诉我,如果你发现错误,或者建议我该怎么做。

please tell me if you find the error or suggest me what to do.

推荐答案

我觉得你清除数据。

this.tweets.clear();
this.users.clear();

.. 我觉得作为鸣叫和用户双方具有相同的refreance(从活动适配器通过),因此两者的鸣叫和两个用户(在适配器上的活动都指向相同的ArrayList),所以无论是GET-清楚,所以这行没有什么意义既空

.. I think as tweets and users both has same refreance ( as passed from activity to adapter ) so both tweets and both users (on adapter and on activity are pointing to same ArrayLists) so both are get-clear so these line not make sense as both are empty

this.tweets.addAll(tweets);
this.users.putAll(users);

尝试

public TweetAdapter(Context context, ArrayList<Tweet> tweets, HashMap<String, User> users) { 

mInflater = LayoutInflater.from(context); 
this.tweets = new ArrayList<Tweet>(); 
this.users = new HashMap<String, User>();

this.tweets.addAll(tweets);
this.users.putAll(users);

Collections.sort(this.tweets, new TweetTimeComparator()); 
} 

..

public void refresh(ArrayList<Tweet> tweets, HashMap<String, User> users) {

  this.tweets.clear();
    this.users.clear();

    this.tweets.addAll(tweets);
    this.users.putAll(users);
    Collections.sort(this.tweets, new TweetTimeComparator());
    notifyDataSetChanged();
}

但要注意:现在参考这两个微博(上适配器和活动)和两个用户(在适配器上的活动)是不同的。

but NOTE : now reference to both tweets (on adapter and on activity) and both users (on adapter and on activity) are different.

应该没有直接赋值像adapter.tweets = activity.tweets任何其他的地方使用this.tweets.addAll(微博);

There should no direct assignment like adapter.tweets = activity.tweets any where other use this.tweets.addAll(tweets);