0

I try to implement homeline pagination via Twitter4j to pull-to-refresh list from Chris Banes. However, I have problems of that how to realize it. I have some notes how it should work but it isn't so, my pull refresh list doesn`t refresh. Have any ideas how to upload next 40 tweet to list on refresh?

Activity

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);
        }
    }

My 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;}
    }
}
4

1 回答 1

1

所以发生的事情是您正在更新tweets变量,这将更新适配器的this.values值,但是该列表不是适配器用来呈现列表的(如果您想知道原因,只需深入研究 ArrayAdapter 代码)。解决问题(并防止进一步混淆)的最简单方法是从 BaseAdapter 扩展。这对您来说需要做更多的工作,但您将完全控制适配器的功能(并且会更好地了解正在发生的事情)。

要进一步清理代码,您还应该向适配器添加一个更新 值的方法this.values,您不应该依赖它引用与 相同的列表tweets

于 2012-05-10T15:44:45.083 回答