我尝试通过Twitter4j实现homeline分页,从Chris Banes那里获取刷新列表。但是,我有如何实现它的问题。我有一些注意它应该如何工作,但事实并非如此,我的拉动刷新列表并没有刷新。有什么想法如何在刷新时将下一条40条推文上传到列表中?
活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tweetlist);
initializeVars();
paging = new Paging(1, 40);
try {
ListView actualListView = pullToRefreshView.getRefreshableView();
tweets = twitter.getHomeTimeline(paging);
tweetAdapter = new TweetListAdapter(this, R.layout.customtweetlist, tweets);
actualListView.setAdapter(tweetAdapter);
} catch (TwitterException e) {
e.printStackTrace();
}
}
public void onRefresh() {
new GetDataTask().execute();
}
private class GetDataTask extends AsyncTask<Void, Void, List<Status>> {
protected List<twitter4j.Status> doInBackground(Void... params) {
paging.setPage(2);
try {
tweets = twitter.getHomeTimeline(paging);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tweets;
}
protected void onPostExecute(List<twitter4j.Status> result) {
tweetAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
pullToRefreshView.onRefreshComplete();
super.onPostExecute(result);
}
}
我的TweetAdapter
public class TweetListAdapter extends ArrayAdapter<Status> {
private final Context context;
private final List<Status> values;
public TweetListAdapter(Context context,int textViewResourceId, List<Status> tweets) {
super(context, textViewResourceId, tweets);
this.context = context;
this.values = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.customtweetlist, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.tvText);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivImage);
Status tweet = values.get(position);
textView.setText(tweet.getText());
// imageView.setImageBitmap(getBitmap(tweet.getProfileImageUrl()));
rowView.invalidate();
return rowView;
}
public static Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection() .getInputStream());
}
catch(Exception ex) {return null;}
}
}
答案 0 :(得分:1)
正在发生的事情是你正在更新tweets
变量,这将更新适配器的this.values
值,但该列表不适配器正在使用的是什么渲染列表(如果你想知道原因,只需深入研究ArrayAdapter代码)。解决问题(并防止进一步混淆)的最简单方法是从BaseAdapter扩展。这对你来说更有用,但是你可以完全控制适配器的功能(并且会更好地理解发生了什么)。
要再清理代码,您还应该在适配器中添加一个更新this.values
值的metod,您不应该依赖它来引用与tweets
相同的列表。< / p>