2012-06-27 40 views
0

我想要做这样的事情。通过notifyDataSetChanged()修改/黑屏;

同时按刷新按钮,它会刷新列表。活动代码是这样的:

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


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

refreshAdapter()当我需要更改列表项目时调用。这里tweets & usersArrayList & HashMap项目

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

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();不工作。通过点击刷新该列表要么变黑(最大时间),要么没有响应(某个时间)。

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

回答

1

我想你清除数据

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

.. 我觉得微博和用户都具有相同的refreance(从活动到适配器通过),所以两者的鸣叫和两个用户(在适配器和活动都指向相同的ArrayList),所以两者都获得清晰所以这些线路没有意义的,因为已空

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

但要注意:现在,参照上述两个鸣叫(在适配器和活动)和两个用户(在适配器和活动)是不同的。

不应该像adapter.tweets = activity.tweets这样的任何地方使用this.tweets.addAll(tweets);

+0

那么该怎么办? this.users.clear();不是太重要,因为我可以使用旧的用户设置,但this.tweets.clear();对于刷新新推文很重要。 –

+0

首先想看到你的适配器上的构造,以确认所有... –

+0

'公共TweetAdapter(上下文的背景下,ArrayList的鸣叫, \t \t \t的HashMap <字符串,用户>用户){ \t \t mInflater = LayoutInflater.from(上下文); \t \t this.tweets = tweets; \t \t this.users = users; \t \t Collections.sort(this.tweets,new TweetTimeComparator()); \t}' –