0

在我的应用程序中,当没有 WiFi 连接时,我使用 AlertDialog 打开 WiFi 连接,然后调用 WiFi 设置屏幕。当我点击后退按钮时,我假设它进入 MainActivity 的 onResume() 方法。在这个方法中,我调用了一个 loadTweets 方法,它会重新加载我的推文。当我从一项活动跳到另一项活动时,这很有效。

当我在 WiFi 设置屏幕中回击时,调试器进入 onResume 方法,循环通过 loadTweets 方法,但它不显示重新加载的推文。

我在这里错过了一步吗?

onResume() 方法:

public void onResume(){
    super.onResume();
    checkWifi();   
    loadTweets();
}

加载推文方法:

private ArrayList<Tweets> loadTweets(){
ArrayList<Tweets> tweets = new ArrayList<Tweets>();
try {
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new
        HttpGet("http://search.twitter.com/search.json?q=JobXXL_be&rpp=5");
        HttpResponse rp = hc.execute(get);
        if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
        {
                String result = EntityUtils.toString(rp.getEntity());
                JSONObject root = new JSONObject(result);
                JSONArray sessions = root.getJSONArray("results");
                for (int i = 0; i < sessions.length(); i++) {
                        JSONObject session = sessions.getJSONObject(i);
                        Tweets tweet = new Tweets();
                        tweet.setTweet(session.getString("text"));
                        tweet.setUser(session.getString("from_user"));
                        tweet.setDate(session.getString("created_at").substring(5,16));
                        tweets.add(tweet);
                }
        }
} catch (Exception e) {
        Log.e("TwitterFeedActivity", "Error loading JSON", e);
 }
 return tweets;
}

TweetListAdapter 类:

private class TweetListAdaptor extends ArrayAdapter<Tweets> {
                private ArrayList<Tweets> tweets;
                public TweetListAdaptor(Context context, int textViewResourceId, ArrayList<Tweets> tweets) {
                         super(context, textViewResourceId, tweets);
                         this.tweets = loadTweets();
                }
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        View v = convertView;
                        if (v == null) {
                                LayoutInflater vi = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
                                v = vi.inflate(R.layout.tweetitem, null);
                        }
                        Tweets o = tweets.get(position);
                        TextView tt = (TextView) v.findViewById(R.id.TWEET_TEXT);
                        TextView bt = (TextView) v.findViewById(R.id.TWEET_SCREEN_NAME);
                        TextView date = (TextView) v.findViewById(R.id.TWEET_CREATED_AT);
                        tt.setText(o.getTweet());
                        bt.setText(o.getUser());
                        date.setText(o.getDate());
                        return v;
                }

                public void setTweets(ArrayList<Tweets> tweet){
                    this.tweets = tweet;
                }
            }
4

1 回答 1

1

Nothing in this code deals with showing your tweets. It only retrieves tweets, but you don't show them in onResume, do you? When you hop between activities the onCreate and onStart methods are invoked, probably you show your tweets somewhere there.

于 2011-05-09T14:52:03.057 回答