Example #1
0
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      ViewHolder holder;

      if (convertView == null) {
        convertView =
            inflater.inflate(
                (getItemViewType(position) == 0
                    ? R.layout.item_youtube_search
                    : R.layout.item_twitter_search),
                parent,
                false);
        holder = new ViewHolder();
        holder.image_iv = (ImageView) convertView.findViewById(R.id.thumbnail_iv);
        holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
        holder.screenName_tv = (TextView) convertView.findViewById(R.id.screen_name_tv);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      if (searchMode == Var.SEARCH_YOUTUBE) {
        YoutubeChannel feed = (YoutubeChannel) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_youtube_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getName());
      }

      if (searchMode == Var.SEARCH_TWITTER) {
        TwitterFeed feed = (TwitterFeed) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_twitter_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getDisplayName());
        holder.screenName_tv.setText(feed.getName());
      }

      if (searchMode == Var.SEARCH_YT_CHANNEL) {
        YoutubeFeed feed = (YoutubeFeed) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_youtube_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getName());
      }

      return convertView;
    }
Example #2
0
    protected void onPostExecute(Integer result) {
      if (isCancelled()) return;
      else {

        if (users == null || twitterPage == 1) {
          twitterFeedSearch.clear();
        }

        try {

          if (users != null) {
            for (twitter4j.User u : users) {
              TwitterFeed feed = new TwitterFeed(String.valueOf(u.getId()));
              feed.setDisplayName(u.getName());
              feed.setChannelHandle("@" + u.getScreenName());
              feed.setThumbnail(u.getProfileImageURL());

              twitterFeedSearch.add(feed);
            }

            searchBusy = false;
          } else if (jsonTokenStream != null) {

            //  Log error and clear the adapter
            Object json = new JSONTokener(jsonTokenStream).nextValue();
            if (json instanceof JSONObject) {
              if (Var.isJsonArray(((JSONObject) json), "errors")) {
                JSONArray errors = ((JSONObject) json).getJSONArray("errors");
                if (errors.length() > 0) {
                  Log.d(TAG, jsonTokenStream);

                  if (Var.isJsonString(errors.getJSONObject(0), "message")) {
                    String message = errors.getJSONObject(0).getString("message");
                    int code = errors.getJSONObject(0).getInt("code");

                    searchMessage_tv.setVisibility(View.VISIBLE);
                    if (code == 88) searchMessage_tv.setText(R.string.rate_limit_error);
                    if (code == 34) searchMessage_tv.setText(R.string.page_does_not_exist_error);
                    else searchMessage_tv.setText(message);
                  }
                }
              }
            } else if (json instanceof JSONArray) {

              if (!twitter.hasAccessToken()) {

                JSONArray results = new JSONArray(jsonTokenStream);
                for (int i = 0; i < results.length(); i++) {
                  JSONObject item = results.getJSONObject(i);

                  if (Var.isJsonString(item, "id")) {

                    TwitterFeed feed = new TwitterFeed(item.getString("id"));
                    if (Var.isJsonString(item, "name")) feed.setDisplayName(item.getString("name"));
                    if (Var.isJsonString(item, "screen_name"))
                      feed.setChannelHandle("@" + item.getString("screen_name"));
                    if (Var.isJsonString(item, "profile_image_url"))
                      feed.setThumbnail(
                          item.getString("profile_image_url").replace("_normal", "_bigger"));

                    twitterFeedSearch.add(feed);
                  }
                }
              }
            }
          }
          searchAdapter.notifyDataSetChanged();
        } catch (Exception e) {
          Log.e("Tweet", "Error retrieving JSON stream" + e.getMessage());
          e.printStackTrace();
        }
      }
    }