我是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<Tweet> 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/