java - 获取 TweetTimelineListAdapter 的数据

标签 java android listview twitter twitter-fabric

我是java新手,所以如果我的问题让你感到困惑,我很抱歉。

我的问题是,我通过 TweetTimelineListAdapter 适配器 获取推文列表,并在 listview 中显示数据,如下所示

final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(userTimeline)
                .build();
setListAdapter(adapter);

一切工作正常,但我想要的是在适配器内获取(查看)数据。我只能在 listview 中看到推文。
我需要将该适配器的数据放入数组中,以便通过 MainActivity 进一步使用。

有什么办法可以做到这一点,或者您可以提供任何建议,请分享。

最佳答案

通常,在 Android 中,每个适配器类都为您提供以下方法:

public int getCount();
public T getItem(int position);

因此,用户可以从 Adapter 类获取所有数据,如下所示:

ArrayList<T> data= new ArrayList<T> ();
for(int i= 0; i < adapter.getCount(); i++) {
    data.add(adapter.getItem(i));
}

所以在你的情况下:

这必须是取自此 github url 的 TweetTimelineListAdapter 代码

package com.twitter.sdk.android.tweetui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.internal.TimelineDelegate;

/**
 * TweetTimelineListAdapter is a ListAdapter which can provide Timeline Tweets to ListViews.
 */
public class TweetTimelineListAdapter extends TimelineListAdapter<Tweet> {
    protected Callback<Tweet> actionCallback;
    final protected int styleResId;

    /**
     * Constructs a TweetTimelineListAdapter for the given Tweet Timeline.
     * @param context the context for row views.
     * @param timeline a Timeline&lt;Tweet&gt; providing access to Tweet data items.
     * @throws java.lang.IllegalArgumentException if timeline is null
     */
    public TweetTimelineListAdapter(Context context, Timeline<Tweet> timeline) {
        this(context, timeline, R.style.tw__TweetLightStyle, null);
    }

    TweetTimelineListAdapter(Context context, Timeline<Tweet> timeline, int styleResId,
            Callback<Tweet> cb) {
        this(context, new TimelineDelegate<>(timeline), styleResId, cb);
    }

    TweetTimelineListAdapter(Context context, TimelineDelegate<Tweet> delegate, int styleResId,
            Callback<Tweet> cb) {
        super(context, delegate);
        this.styleResId = styleResId;
        this.actionCallback = new ReplaceTweetCallback(delegate, cb);
    }

    /**
     * Returns a CompactTweetView by default. May be overridden to provide another view for the
     * Tweet item. If Tweet actions are enabled, be sure to call setOnActionCallback(actionCallback)
     * on each new subclass of BaseTweetView to ensure proper success and failure handling
     * for Tweet actions (favorite, unfavorite).
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        final Tweet tweet = getItem(position);
        if (rowView == null) {
            final BaseTweetView tv = new CompactTweetView(context, tweet, styleResId);
            tv.setOnActionCallback(actionCallback);
            rowView = tv;
        } else {
            ((BaseTweetView) rowView).setTweet(tweet);
        }
        return rowView;
    }

    /*
     * On success, sets the updated Tweet in the TimelineDelegate to replace any old copies
     * of the same Tweet by id.
     */
    static class ReplaceTweetCallback extends Callback<Tweet> {
        TimelineDelegate<Tweet> delegate;
        Callback<Tweet> cb;

        ReplaceTweetCallback(TimelineDelegate<Tweet> delegate, Callback<Tweet> cb) {
            this.delegate = delegate;
            this.cb = cb;
        }

        @Override
        public void success(Result<Tweet> result) {
            delegate.setItemById(result.data);
            if (cb != null) {
                cb.success(result);
            }
        }

        @Override
        public void failure(TwitterException exception) {
            if (cb != null) {
                cb.failure(exception);
            }
        }
    }

    /**
     * TweetTimelineListAdapter Builder
     */
    public static class Builder {
        private Context context;
        private Timeline<Tweet> timeline;
        private Callback<Tweet> actionCallback;
        private int styleResId = R.style.tw__TweetLightStyle;

        /**
         * Constructs a Builder.
         * @param context Context for Tweet views.
         */
        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Sets the Tweet timeline data source.
         * @param timeline Timeline of Tweets
         */
        public Builder setTimeline(Timeline<Tweet> timeline) {
            this.timeline = timeline;
            return this;
        }

        /**
         * Sets the Tweet view style by resource id.
         * @param styleResId resource id of the Tweet view style
         */
        public Builder setViewStyle(int styleResId) {
            this.styleResId = styleResId;
            return this;
        }

        /**
         * Sets the callback to call when a Tweet action is performed on a Tweet view.
         * @param actionCallback called when a Tweet action is performed.
         */
        public Builder setOnActionCallback(Callback<Tweet> actionCallback) {
            this.actionCallback = actionCallback;
            return this;
        }

        /**
         * Builds a TweetTimelineListAdapter from Builder parameters.
         * @return a TweetTimelineListAdpater
         */
        public TweetTimelineListAdapter build() {
            return new TweetTimelineListAdapter(context, timeline, styleResId, actionCallback);
        }
    }
}

您的使用方式如下:

final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(userTimeline)
                .build();
setListAdapter(adapter);

这里TweetTimelineListAdapter扩展了TimelineListAdapter

TimelineListAdapter扩展了BaseAdapter

BaseAdapter 类具有以下 methods :

public int getCount();
public T getItem(int position);

这样你就可以从TweetTimelineListAdapter获取所有数据,如下所示:

ArrayList<Tweet> tweets = new ArrayList<Tweet> ();
for(int i= 0; i<adapter.getCount();i++) {
    tweets.add(adapter.getItem(i));
}

在这里,您会将来自 TweetTimelineListAdapter 的所有推文放入数组列表中。

关于java - 获取 TweetTimelineListAdapter 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37452310/

相关文章:

即使在定义 equals 之后,Java Contains 方法也会返回 false

java - XML 响应时间 ejabberd

javascript - jquery mobile listview 不像演示页面那样工作

java - 无法将数组发送到android中的listview

java - Hibernate:删除实体网络中的实体

java - 类的另一个对象的行为不同,JavaSource_Calendar 类

android - Branch Deep Link 打不开应用

android - Android Lollipop 上的 android-ffmpeg 问题

android - 后退按钮重新启动 Activity 而不是返回

java - 我如何从 firebase 检索数据到 android listview 的顶部