简体   繁体   English

for循环中的for循环 - ListArrays - Android,Java!

[英]For-loop inside a For-loop - ListArrays - Android, Java!

This is a noob question but I have to ask it anyway because I think I'll go crazy soon. 这是一个菜鸟问题,但无论如何我都要问它,因为我想我很快就会发疯。

What I want to achieve is a ListView that populates by twitter and this I have implemented, the two ListArray s gets populated by the tweet and the picture URL. 我想要实现的是一个由twitter填充的ListView ,我已经实现了,这两个ListArray由tweet和图片URL填充。 This is successful, but when I refresh twitter the new pile of twitter gets below the old ones. 这是成功的,但是当我刷新推特时,新的推特会低于旧的推特。

I could simply clear the ListArrays but then all the twitter disappears in the list which is not desirable, I simply want to add only the new tweets, and make the new tweets get on top of the list. 我可以简单地清除ListArrays但是然后所有的twitter都会在列表中消失,这是不可取的,我只是想添加新的推文,并使新的推文在列表之上。

So I guess this is a two fold question 所以我想这是一个双重问题

  1. How do I make the list only to add which is not there?, and 如何使列表仅添加哪些不存在?,和
  2. How do I make the latest tweet appear on top? 如何让最新的推文出现在最顶层?

Could really use some great input, the code below is the most essential part of the (messy) code, which is the refresh method. 真的可以使用一些很棒的输入,下面的代码是(凌乱)代码中最重要的部分,也就是刷新方法。

public void updateTwitter(){

        twitter = new TwitterFactory().getInstance();

        try {
            QueryResult result = twitter.search(new Query(TWEETQUERY));
            tweets = result.getTweets();
            for (Tweet tweet : tweets) {
                String tweetProfile = tweet.getProfileImageUrl();
                String twwwwww = tweet.getText();
                for(String tww : tweetString){

                    if(tww.equals(twwwwww)){
                        System.out.println("Not added");
                    } else {
                        tweetString.add(twwwwww);
                        urlArray.add(tweetProfile);
                    }
                }
            }
            if(adapter != null) {
                tweetList.setAdapter(adapter);
                System.out.println("adapter sets, not created");
            } else {
                adapter = new LazyAdapter(this, urlArray, tweetString);
                tweetList.setAdapter(adapter);
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());
        }
    }

创建一个从BaseAdapter扩展并调用notifyDataSetChanged()的适配器。

It sounds like you really want a set, not a list, and be able to traverse it according to some criteria (time added, in this case). 听起来你真的想要一个集合,而不是列表,并且能够根据某些标准遍历它(在这种情况下添加时间)。 Perhaps a SortedSet would be appropriate? 也许SortedSet会合适吗?

The implementation would be something like this: 实现将是这样的:

/* SortedSet<Tweet> tweets defined somewhere above, possibly with a custom Comparator */

public void updateTwitter()
{
   try
   {
       tweets.addAll(TwitterFactory.getInstance().search(query).getTweets());
   } catch (TwitterException te) {
       te.printStackTrace();
       System.out.println("Failed to search tweets: " + te.getMessage());
   }
}

Presumably, the LazyAdapter derives from BaseAdapter and is backed by urlArray . 据推测,LazyAdapter派生自BaseAdapter,并由urlArray支持。 If so, to have tweets appear on top, you need to add them to the beginning of the array. 如果是这样,要将推文显示在顶部,您需要将它们添加到数组的开头。

Use add(0, newEntry) to add to the beginning of the array and notifyDataSetChanged() to tell the adapter to post the updates . 使用add(0, newEntry) 添加到数组的开头,使用 notifyDataSetChanged() 告诉适配器发布更新

A couple of other notes: 其他几点说明:

1) Its unclear to me what you intend, especially seeing code like twww.equals(twwwwwww) . 1)我不清楚你的意图,特别是看到像twww.equals(twwwwwww)这样的代码。 It seems like you're maintaining the old tweets and trying to only add new ones, but with unclear variable names like twww, twwwwww, and tweetString , I can only guess. 看起来你正在维护旧的推文,并试图只添加新的推文,但有不明确的变量名称,如twww, twwwwww, and tweetString ,我只能猜测。

2) You shouldn't have to keep setting the adapter. 2)您不必继续设置适配器。 Once the relationship between the listview, adapter, and backing array is set, you should only have to update the array and notify the adapter. 设置listview,适配器和后备阵列之间的关系后,您只需更新阵列并通知适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM