1

How's everyone doing, for the past week I have been trying to figure out what exactly it means to wrap my adapter with the endlessadapter without success. I've tried all my limited knowledge allows, so far I'm loosing the battle. I've read a couple of examples online plus read the endlessadapter instructions, but it did not really help. can someone please shed some light on the meaning of wrapping my adapter.

My list view is as follows:

package com.bopbi.ui;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.bopbi.R;
import com.bopbi.model.LocationAdapter;
import com.bopbi.model.LocationList;
import com.bopbi.model.LocationModel;
import com.bopbi.model.PullToRefreshListView.OnRefreshListener;
import com.bopbi.util.RestClient;
import com.bopbi.model.PullToRefreshListView;
import com.bopbi.util.RestClient.RequestMethod;
import com.google.android.maps.GeoPoint;
import com.google.gson.Gson;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListRest extends ListActivity {


LocationManager lm;
GeoPoint userLocation;

//static String[] TITLE;
//static String[] DESCRIPTION;

ArrayList<LocationModel> locationArray = null;
LocationAdapter locationAdapter;
LocationList list;

//PullToRefreshListView lv;
TextView loadingText;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
  //  actionBar.show();
  //  actionBar.setDisplayShowHomeEnabled(true);

    setContentView(R.layout.activity_home);

   // lv = (PullToRefreshListView) findViewById(R.id.list);

    locationArray = new ArrayList<LocationModel>();
    locationAdapter = new LocationAdapter(ListRest.this, R.layout.dublet, locationArray);
    ((PullToRefreshListView) getListView()).onRefresh();

 //   lv.setTextFilterEnabled(true);
    setListAdapter(locationAdapter);
    //this.getListView().scrollTo(0,160);
    //this.getListView().scrollTo(0,0);
    // Set a listener to be invoked when the list should be refreshed.
    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.
            try {
                new LocationSync().execute("http://exposezvous.com/list.php");
            } catch(Exception e) {} 
        }
    });



        try {
            new LocationSync().execute("http://exposezvous.com/list.php");
        } catch(Exception e) {} 



}





private class LocationSync extends AsyncTask<String, Integer, LocationList> {

    @Override
    protected void onPreExecute() {

}   

    protected LocationList doInBackground(String... urls) {
        LocationList list = null;
        int count = urls.length;

        for (int i = 0; i < count; i++) {
            try {           
                // ntar diganti service
                RestClient client = new RestClient(urls[i]);

                try {
                    client.Execute(RequestMethod.GET);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String json = client.getResponse();

                list = new Gson().fromJson(json, LocationList.class);

                //
            } catch(Exception e) {}
        }
        return list;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(LocationList loclist) {

        // Obtain MotionEvent object
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        float x = 1.0f;
        float y = 10.0f;
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        int metaState = 0;
        MotionEvent motionEvent = MotionEvent.obtain(
            downTime, 
            eventTime, 
            MotionEvent.ACTION_MOVE, 
            x, 
            y, 
            metaState
        );  

        for(LocationModel lm : loclist.getLocations())
       {
     locationArray.add(lm);
        }
        locationAdapter.notifyDataSetChanged();
     // Call onRefreshComplete when the list has been refreshed.

   ((PullToRefreshListView) getListView()).onRefreshComplete();
    ((PullToRefreshListView) getListView()).onTouchEvent(motionEvent);

    ((PullToRefreshListView) getListView()).setLastUpdated("Last Updated on Jan 24, 2013 8:27 PM");
    }

}
}

My custom Adapter is as follows:

package com.bopbi.model;


import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bopbi.R;


public class LocationAdapter extends  ArrayAdapter<LocationModel> {

int resource;
String response;
Context context;
private LayoutInflater mInflater;
private final ImageDownloader imageDownloader = new ImageDownloader();
public int count;

public LocationAdapter(Context context, int resource,
        List<LocationModel> objects) {
    super(context, resource, objects);
    this.resource = resource;
    mInflater = LayoutInflater.from(context);
}

static class ViewHolder {
    TextView username;
    ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    // Get the current location object
    LocationModel lm = (LocationModel) getItem(position);

    // Inflate the view
    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.dublet, null);
        holder = new ViewHolder();



        holder.username = (TextView) convertView
                .findViewById(R.id.username);
        holder.image = (ImageView) convertView
                .findViewById(R.id.profilePic);

        // .findViewById(R.id.it_location_description);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.username.setText(lm.getName());
    //String url = "http://***********/small_profile_pic_white_bigger.jpg";


    ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
    imageDownloader.setMode(mode);
    imageDownloader.download(lm.getImage(), holder.image);
    return convertView;
}




}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Nick
  • 59
  • 2
  • 9
  • 1
    *what exactly it means to wrap my adapter with the endlessadapter* - what exactly do you want to know?. What is the purpose of that MotionEvent that you create in the `onPostExecute`? – user Jan 26 '13 at 09:32
  • Hi! Welcome to StackOverflow! StackOverflow is for programming questions, and this is not a question. – CommonsWare Jan 26 '13 at 12:40
  • CommonsWare I'm not exactly sure what you mean by this not being a programming question. Maybe I worded it incorrectly, the online document says that endlessadapter is designed to wrap around another adapter. I supplied my LocationAdapter to the EnlessAdapter but I received constructor errors. I then edited my contructor to match the endlessadapter, but does not know how to proceed with an ArrayAdapter. – Nick Jan 27 '13 at 01:33

1 Answers1

3

If I understand you correctly, you're attempting to use the EndlessAdapter with your LocationAdapter.

The documentation for Commonsware's EndlessAdapter mentions that one of the constructors offered by EndlessAdapter takes an existing ListAdapter as its parameter. Wrapping your adapter involves passing your LocationAdapter to a subclass of EndlessAdapter as a parameter.

private class EndlessLocationAdapter extends EndlessAdapter
{
  public EndlessLocationAdapter(LocationAdapter adapter)
    {
    super(adapter);
    }

  @Override
  protected boolean cacheInBackground() throws Exception
    {
    //Fetch more data
    }
//Other methods such as getPendingView(), appendCachedData()
}

In your activity

locationArray = new ArrayList<LocationModel>();
LocationAdapter locationAdapter = new LocationAdapter(ListRest.this, R.layout.dublet, locationArray);
EndlessLocationAdapter endlessLocAdapter = new EndlessLocationAdapter(locationAdapter);
setListAdapter(endlessLocAdapter);
WeNeigh
  • 3,489
  • 3
  • 27
  • 40
  • Thanks WeNeigh, this shed some lights on what exactly it is I should be doing. I'm trying out your code and I'm currently getting that EndlessAdapter cannot be instantiated because it is an Abstract class. I'll post my resolutions once I figure it out. – Nick Jan 27 '13 at 01:54
  • `locationArray = new ArrayList(); LocationAdapter locationAdapter = new LocationAdapter(ListRest.this, R.layout.dublet, locationArray); EndlessAdapter endlessLocationAdapter = new EndlessLocationAdapter(locationAdapter); setListAdapter(endlessLocationAdapter);` I've changed the ListRest code to this and implemented the getPendingView in the EndlessLocationAdapter, but I'm getting a NULLPOINTER error in my executePost for LocationAdapter. – Nick Jan 27 '13 at 02:24
  • Ok, I got the program loading now. What I did wrong was that I was reinitializing LocationManager. Changed this `LocationAdapter locationAdapter = new LocationAdapter(ListRest.this, R.layout.dublet, locationArray);` to this `locationAdapter = new LocationAdapter(ListRest.this, R.layout.dublet, locationArray);` – Nick Jan 27 '13 at 02:33
  • I'm a little confused, is cacheInBackground() wanting pagination or my whole dataset. – Nick Jan 27 '13 at 17:45
  • Use cacheInBackground() to fetch one unit of data, and appendCachedData() to add it to the existing dataset. E.g: If your app is currently showing 15 images, and you choose to fetch 5 images at a time, use cacheInBackground to download the 5 new images, and appendCachedData to add them to the existing list of 15 images (making a total of 20 images in the list after appendCachedData is executed) – WeNeigh Jan 27 '13 at 19:03
  • 2
    Does anyone have an example of a rest call being made within cacheInBackground()? do I need to have an AsyncTask within the same class? – Nick Jan 29 '13 at 01:26
  • Refer to the documentation for EndlessAdapter. It says that cacheInBackground will be called from a background thread, so there is no need for you to start off another thread to make the REST call. – WeNeigh Jan 29 '13 at 08:46