public static ArrayList<TwitterEvent> get() { ArrayList<TwitterEvent> events = new ArrayList<TwitterEvent>(); try { FeedReader reader = new FeedReader(); reader.execute(Server.TWITTER_FEED_URL); JSONObject jsonResponse; if (reader.get() != null) { jsonResponse = new JSONObject(reader.get()); JSONArray jsonMainNode = jsonResponse.optJSONArray("twitter_event"); for (int i = 0; i < jsonMainNode.length(); i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); TwitterEvent event = new TwitterEvent(); event.setName(jsonChildNode.optString("name")); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = null; try { date = dateFormat.parse(jsonChildNode.optString("date")); } catch (ParseException e) { e.printStackTrace(); } Calendar gregorianCalendar = Calendar.getInstance(); gregorianCalendar.setTime(date); event.setTime((GregorianCalendar) gregorianCalendar); event.setHashTags(jsonChildNode.optString("hashtags")); events.add(event); } } else { return new ArrayList<TwitterEvent>(0); } } catch (JSONException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return events; }
@Override public View getView(int position, View convertView, ViewGroup parent) { int viewType = this.getItemViewType(position); java.text.DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(activity.getApplicationContext()); java.text.DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity.getApplicationContext()); switch (viewType) { case 0: GoogleViewHolder google_holder = null; if (convertView == null) { LayoutInflater inflater = activity.getLayoutInflater(); convertView = inflater.inflate(R.layout.google_item, parent, false); google_holder = new GoogleViewHolder(); google_holder.textViewGoogleName = (TextView) convertView.findViewById(R.id.textViewGoogleName); google_holder.textViewGoogleTime = (TextView) convertView.findViewById(R.id.textViewGoogleTime); convertView.setTag(google_holder); } else { google_holder = (GoogleViewHolder) convertView.getTag(); } GoogleEvent google_event = (GoogleEvent) social_events.get(position); if (google_event != null) { if (google_holder.textViewGoogleName != null) { google_holder.textViewGoogleName.setText(google_event.getName()); } if (google_holder.textViewGoogleTime != null) { String time = dateFormat.format(google_event.getTime().getTime()); if (System.currentTimeMillis() < google_event.getTime().getTimeInMillis()) { time += " " + timeFormat.format(google_event.getTime().getTime()); } google_holder.textViewGoogleTime.setText(time); } } return convertView; case 1: TwitterViewHolder twitter_holder = null; if (convertView == null) { LayoutInflater inflater = activity.getLayoutInflater(); convertView = inflater.inflate(R.layout.twitter_item, parent, false); twitter_holder = new TwitterViewHolder(); twitter_holder.textViewTwitterName = (TextView) convertView.findViewById(R.id.textViewTwitterName); twitter_holder.textViewTwitterHashtags = (TextView) convertView.findViewById(R.id.textViewTwitterHashtags); twitter_holder.textViewTwitterTime = (TextView) convertView.findViewById(R.id.textViewTwitterTime); convertView.setTag(twitter_holder); } else { twitter_holder = (TwitterViewHolder) convertView.getTag(); } TwitterEvent twitter_event = (TwitterEvent) social_events.get(position); if (twitter_event != null) { if (twitter_holder.textViewTwitterName != null) { twitter_holder.textViewTwitterName.setText(twitter_event.getName()); } if (twitter_holder.textViewTwitterTime != null) { String time = dateFormat.format(twitter_event.getTime().getTime()); if (System.currentTimeMillis() < twitter_event.getTime().getTimeInMillis()) { time += " " + timeFormat.format(twitter_event.getTime().getTime()); } twitter_holder.textViewTwitterTime.setText(time); } if (twitter_holder.textViewTwitterHashtags != null) { twitter_holder.textViewTwitterHashtags.setText(twitter_event.getHashTags()); } } return convertView; } return parent; }