Tuesday, 27 August 2013

how to show tweets in list view in android?

how to show tweets in list view in android?

i want to show tweets in a list view in android application.i have used
following code but the tweets are not showing.. could anybody pls tell how
to show 5 latest tweets in list view which are reloaded after every 15
minutes? i have tried many codes but did not reached at any result!
public class TweetsListActivity extends ListActivity {
ListView lv;
RequestToken requestToken;
Twitter twitter;
TextView listItem;
static ConfigurationBuilder cb;
private static SharedPreferences mSharedPreferences;
private ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
private static final String CONSUMER_KEY = "**";
private static final String CONSUMER_SECRET = "***";
static String PREFERENCE_NAME = "twitter_oauth";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.tweets_list);
super.onCreate(savedInstanceState);
listItem =(TextView)findViewById(R.id.listItem);
ArrayList tweets = getTweets("android", 1);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new UserItemAdapter(this, R.layout.tweet_item, tweets));
mSharedPreferences =
getApplicationContext().getSharedPreferences("MyPref",
0);
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int textViewResourceId,
ArrayList<Tweet> tweets) {
super(context, textViewResourceId,tweets);
this.tweets=tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi =
(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.tweet_item, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) view.findViewById(R.id.listItem);
TextView message = (TextView) view.findViewById(R.id.message);
if (username != null) {
username.setText(tweet.username);
}
if(message != null) {
message.setText(tweet.message);
}
}
return view;
}
}
private ArrayList<Tweet> getTweets(String searchTerm, int page) {
String searchUrl ="http://search.twitter.com/search.json?q=@"+
searchTerm + "&rpp=100&page=" + page;
ArrayList tweets = new ArrayList();
HttpClient client = (HttpClient) new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
String responseBody = null;
try {
responseBody = ((AbstractHttpClient) client).execute(get,
responseHandler);
} catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
JSONArray ja = null;
try{
ja = jsonObject.getJSONArray("results");
for (int i = 0; i < ja.length(); i++) {
Tweet tweet = new Tweet(
((JSONObject) ja.get(i)).get("from_user").toString(),
((JSONObject) ja.get(i)).get("text").toString()
);
tweets.add(tweet);
}}
catch(Exception e){
}
return tweets;
}
}
any help would be highly appreciated.. thank you.

No comments:

Post a Comment