I'm trying to create an infinite ListView to go looking for information in my webservice every 10 minutes.
For this I'm trying to use this example: link
But I do not know how I can return from this example the offset and the limit to go sending to the webservice and it returns the amount of records I need.
EndlessScrollListener
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
onLoadMore(currentPage + 1, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
Fragment
public class NoticiaFrag extends Fragment {
private ListView lvNoticias;
private List<Noticia> listaNoticias = new ArrayList<Noticia>();
private NoticiaListAdapter noticiaLA;
private static final String TAG = "NoticiaFrag";
protected ProgressDialog progressDialog;
private static Integer OFFSET = 0, LIMIT = 10;
//singleton
private static NoticiaFrag mFrag;
public static NoticiaFrag newInstance() {
if(mFrag == null){
mFrag = new NoticiaFrag();
}
return mFrag;
}
public NoticiaFrag() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.noticia_frag, container, false);
lvNoticias = (ListView)view.findViewById(R.id.lvNoticias);
lvNoticias.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
customLoadMoreDataFromApi(page);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
customLoadMoreDataFromApi(OFFSET);
}
// Append more data into the adapter
public void customLoadMoreDataFromApi(int offset) {
OFFSET = (offset - 1) * LIMIT;
LIMIT = (2 * OFFSET);
Log.i("OFFSET", OFFSET + "");
Log.i("LIMIT", LIMIT + "");
progressDialog = new CustomProgressDialog().getCustomProgress(null, getView().getContext());
progressDialog.show();
try {
JsonObjectRequest app = new NoticiaDAO().getAllNoticias(LIMIT, OFFSET, new NoticiasAdapter() {
@Override
public void getAllNoticias(List<Noticia> lista) {
Log.w("SIZELIST->", lista.size()+ "") ;
if(!lista.isEmpty()){
listaNoticias = lista;
noticiaLA.changeLista(listaNoticias);
}else{
Toast.makeText(getView().getContext(), "Nenhuma noticia encontrada", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
CustomVolleySingleton.getInstance().addToRequestQueue(app);
}catch (Exception e ){
Log.e("ERROR: " + TAG, "Method: " + "getAllNoticias: " + e);
}
}
@Override
public void onResume() {
super.onResume();
ChangeActionBar.changeActionBar(getActivity(), "Notícias", false, "");
}
@Override
public void onPause() {
super.onPause();
CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG);
ChangeActionBar.changeActionBar(getActivity(), null, false, "");
}
}
NewsListAdapter
public class NoticiaListAdapter extends BaseAdapter {
private Context context;
private List<Noticia> lista;
public NoticiaListAdapter(Context context, List<Noticia> lista) {
this.context = context;
this.lista = lista;
}
public void changeLista(List<Noticia> lista){
for(Noticia n : lista){
this.lista.add(n);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Noticia noticia = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.noticia_listadapter, parent, false);
holder.llNoticiaListAdapter = (LinearLayout) convertView.findViewById(R.id.llNoticiaListAdapter);
holder.sivNoticia = (SmartImageView)convertView.findViewById(R.id.sivNoticia);
holder.tvTitulo = (TextView) convertView.findViewById(R.id.tvTitulo);
holder.wvDescricao = (WebView) convertView.findViewById(R.id.wvDescricao);
holder.tvData = (TextView) convertView.findViewById(R.id.tvData);
holder.tvAutor = (TextView) convertView.findViewById(R.id.tvAutor);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if(noticia.getImage() == null){
holder.sivNoticia.setImageDrawable(context.getResources().getDrawable(R.drawable.no_image_news));
}else{
holder.sivNoticia.setImageUrl(WebServiceURL.getBaseWebServiceURL() + "app/webroot/img/noticias/" + noticia.getImage());
}
holder.tvTitulo.setText(noticia.getTitulo());
holder.wvDescricao.loadData(formatDescricao(noticia.getDescricao()), "text/html; charset=utf-8", null);
holder.tvData.setText(new DateControl().getDataHoraFormat(noticia.getCreated()));
holder.tvAutor.setText("Por: " + noticia.getUsuario());
return convertView;
}
private String formatDescricao(String descricao){
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<body>");
html.append("<small>");
html.append("<p align=\"justify\" style=\"color:gray\">");
html.append(descricao);
html.append("</p");
html.append("</small>");
html.append("</body>");
html.append("</html>");
return html.toString();
}
private static class ViewHolder{
LinearLayout llNoticiaListAdapter;
SmartImageView sivNoticia;
TextView tvTitulo;
WebView wvDescricao;
TextView tvData;
TextView tvAutor;
}
}