In my app, when there's no WiFi connection, I turn the WiFi connection on with an AlertDialog and I call the WiFi Settings screen. When I hit the back button, I assume that it goes in the onResume() method of the MainActivity. In this method, I call a loadTweets method, which reloads my tweets. This works when I hop from one activity to another.
When I hit back in the WiFi Settings Screen, the debugger goes in the onResume method, loops through the loadTweets method, but it doesn't show the reloaded tweets.
Am I missing a step here?
onResume() method:
public void onResume(){
super.onResume();
checkWifi();
loadTweets();
}
LoadTweets method:
private ArrayList<Tweets> loadTweets(){
ArrayList<Tweets> tweets = new ArrayList<Tweets>();
try {
HttpClient hc = new DefaultHttpClient();
开发者_Python百科 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 class:
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;
}
}
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.
精彩评论