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;}
    }
}
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Viktor M.
  • 4,393
  • 9
  • 40
  • 71

1 Answers1

1

So what's happening is that you're updating the tweets variable, which will update the Adapter's this.values value, HOWEVER that list is not what the adapter is using to render the list (if you want to know why, just dig into the ArrayAdapter code). The easiest way to fix the problem (and prevent further confusion) is to extend from BaseAdapter. It's a bit more work for you but you will have full control of what the adapter does (and will understand what's going on better).

To clean the code some more, you should also add a metod to your adapter that updates the value of this.values, you shouldn't depend on it referring to the same list as tweets.

dmon
  • 30,048
  • 8
  • 87
  • 96