Well iv'e been stuck for a few days now and have decided to come here for assistance.
I am using the twitter4j library to load statuses from a usertimeline 10 at a time using a pager (Paging class)
my issue here comes when i am trying to update the listview when it reaches the bottom using the enlessScrollListener , i am successfully getting more tweets and calling the refill method in the adapter but the adapter is refusing to refill,
I noted that when i call the refill method with the new updated list, the old list successfully clears but the addAll() method in the adapter is resulting into an empty list and nothing is available for the adapter to adapt thus resulting to an empty view,.
A weird issue is that in the PostExecute method if i append the new data to the beginning of the list instead of using the addAll() method then the adapter will successfully display the list but in a jumbled order.
If possible I would love to be informed of a better way to load old tweets as user scrolls to the bottom.
Sorry for long post i am new to android development and have literally lost sleep over this haha.
Here is the code from the fragment :
package com.dise.weekone.feed;
import java.util.List;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuth2Token;
import twitter4j.conf.ConfigurationBuilder;
import android.content.Context;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import com.dise.weekone.R;
import com.dise.weekone.adapters.EndlessScrollListener;
import com.dise.weekone.adapters.FeedAdapter;
import com.dise.weekone.util.BaseFragment;
import com.dise.weekone.util.Const;
public class FeedFragment extends BaseFragment {
protected final static String ScreenName = Const.UON_TWITTER_NAME;
// protected Twitter twits;
// protected DownloadTwitterTask downloadTwitterTask;
Paging pager = new Paging(1, 10);
int pagecount = 1;
protected FeedAdapter adapter;
protected ProgressBar progressBar;
protected List<Status> statuses;
protected SwipeRefreshLayout swipeLayout;
protected Typeface defaultFont;
protected DownloadTwitterTask downloadTwitterTask;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_feed, container,
false);
ab.setTitle(R.string.twitter_tab_title);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
progressBar.setVisibility(View.VISIBLE);
swipeLayout = (SwipeRefreshLayout) rootView
.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(mOnRefreshListener);
swipeLayout.setColorScheme(R.color.category_mature_color,
R.color.category_free_color, R.color.category_off_campus_color,
R.color.category_night_out_color);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
downloadTweets();
}
@Override
public void onDetach() {
super.onDetach();
if (downloadTwitterTask != null)
downloadTwitterTask.cancel(true);
}
public void downloadTweets() {
ConnectivityManager connMgr = (ConnectivityManager) mainActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
downloadTwitterTask = new DownloadTwitterTask();
downloadTwitterTask.execute(ScreenName);
} else {
Log.v(null, "No network connection available.");
}
}
protected void adaptData() {
progressBar.setVisibility(View.INVISIBLE);
if (swipeLayout.isRefreshing()) {
Log.i(null, "refresh called");
swipeLayout.setRefreshing(false);
((FeedAdapter) getListView().getAdapter()).refillTweets(statuses);
}
if (getListView().getAdapter() == null) {
Log.i(null, "firstlistview called");
adapter = new FeedAdapter(getActivity(), statuses);
getListView().setOnScrollListener(new endlessScrollListener());
getListView().setAdapter(adapter);
} else {
Log.i(null, "refill called");
Log.i(null, "status amount sent: " + statuses.size());
((FeedAdapter) getListView().getAdapter()).refillTweets(statuses);
}
}
protected class DownloadTwitterTask extends
AsyncTask<String, Void, List<twitter4j.Status>> {
@Override
protected List<twitter4j.Status> doInBackground(String... arg0) {
return getUserTimeline();
}
@Override
protected void onPostExecute(List<twitter4j.Status> result) {
if (statuses != null) {
statuses.addAll(result);
} else {
statuses = result;
}
adaptData();
}
private List<twitter4j.Status> getUserTimeline() {
List<twitter4j.Status> tempStatuses = null;
try {
// gets Twitter instance with default credentials\
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("B4V****************xC")
.setOAuthConsumerSecret(
"skIOI***************************HcR")
.setApplicationOnlyAuthEnabled(true); // IMPORTANT: set
// T4J to use
// App-only auth
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
OAuth2Token token = twitter.getOAuth2Token();
String user = "ScreenNameforTwitterAccount";
pager.setPage(pagecount);
tempStatuses = twitter.getUserTimeline(user, pager);
} catch (TwitterException te) {
te.printStackTrace();
Log.e(null, "failed to get timeline");
}
return tempStatuses;
}
}
public class endlessScrollListener extends EndlessScrollListener {
@Override
public void onLoadMore(int page, int totalItemsCount) {
customLoadMoreDataFromApi(page);
}
// Append more data into the adapter
public void customLoadMoreDataFromApi(int offset) {
// This method probably sends out a network request and appends new
// data items to your adapter.
// Use the offset value and add it as a parameter to your API
// request to retrieve paginated data.
// Deserialize API response and then construct new objects to append
// to the adapter
Log.i(null, "offset = " + offset);
pagecount = offset;
downloadTweets();
}
}
protected OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
@Override
public void onRefresh() {
downloadTweets();
}
};
}
FeedAdapter.java
0 comments:
Post a Comment