1

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 ListArrays gets populated by the tweet and the picture 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.

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

3 Answers 3

2

Make an Adapter that extends from BaseAdapter and call notifyDataSetChanged().

Sign up to request clarification or add additional context in comments.

Comments

1

Presumably, the LazyAdapter derives from BaseAdapter and is backed by 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.

A couple of other notes:

1) Its unclear to me what you intend, especially seeing code like 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.

2) You shouldn't have to keep setting the adapter. Once the relationship between the listview, adapter, and backing array is set, you should only have to update the array and notify the adapter.

2 Comments

Of course you are right, those variable names are confusing to say the least, but I was really tired and my IQ of naming conventions went down. Should have posted a little more cleaner example sorry about that. But of course I should not have to keep setting the adapter, and our suggestions makes perfect sense.
@Joakim - I suffer from the late-night IQ drop on many nights myself. :0
1

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?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.