我是java的新手所以如果我把你的问题困惑,我很抱歉。
我的问题是我通过TweetTimelineListAdapter adapter
获取推文列表并在listview
中显示这样的数据
final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
.setTimeline(userTimeline)
.build();
setListAdapter(adapter);
一切都很好,但我想要的是获取(见)adapter
内的数据。我只能看到listview
中的推文。
我需要将适配器的数据放在一个数组中,以便通过MainActivity
进一步使用。
有没有办法做到这一点或任何你可以提出的建议请分享。
答案 0 :(得分:0)
通常,在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
所有推文到你的arraylist的推文。
答案 1 :(得分:-1)
你应该这样做:
List<Tweet> tweets = new ArrayList<>();
int count = adapter.getCount();
for (int i=0;i<count;i++) {
tweets.add(adpater.getItem(i););
}
我假设get项目返回的对象名为Tweet(从未使用过它)。