Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- this.view = inflater.inflate(R.layout.fragment_list, container, false);
- // create instance of my async task class
- lp = new RestfulConnect(getActivity());
- // set onArrayListReady as a listener.
- lp.setOnListReady(this);
- // do the
- lp.getStationsArray(Integer.toString(this.start));
- return view;
- }
- @Override
- public void onArrayListReady(ArrayList<Station> stations) {
- adapter = new StationListAdapter(getActivity().getApplicationContext(), R.layout.fragment_list_item, stations);
- listView = (ListView) this.view.findViewById(R.id.fragment_list_stations_list);
- listView.setAdapter(adapter);
- // Listener lie a l'action de click sur un objet de la liste
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
- Station station = (Station) listView.getAdapter().getItem(i);
- Intent intent = new Intent(getActivity().getApplicationContext(), Detail.class);
- intent.putExtra("station", station);
- startActivity(intent);
- }
- });
- // Listener qui permet de charger les donnees suivantes
- listView.setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view,
- int scrollState) { // TODO Auto-generated method stub
- int threshold = 1;
- int count = listView.getCount();
- if (scrollState == SCROLL_STATE_IDLE) {
- if (listView.getLastVisiblePosition() >= count - threshold) {
- // WHERE I'D LIKE TO EXECUTE THE PREVIOUS INSTANCE OF LP
- int start = 20;
- this.lp.getStationsArray(Integer.toString(start));
- }
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- // TODO Auto-generated method stub
- }
- });
- }
- public class RestfulConnect {
- // Constructor
- public RestfulConnect(Context context) {
- this.context = context;
- }
- public void getStationsArray(String... filters) {
- String apiUrl = requestUrlBuilder(filters);
- ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
- if(networkInfo != null && networkInfo.isConnected()) {
- new getStationsAsync().execute(apiUrl);
- }
- else {
- Log.d(DEBUG_MAIN_CALL, "Pas de connexion disponible");
- }
- }
- private class getStationsAsync extends AsyncTask<String, Void, ArrayList<Station>> {
- @Override
- protected ArrayList<Station> doInBackground(String... urls) {
- String jsonString;
- JSONObject jsonObject;
- JSONArray jsonArray;
- ArrayList<Station> stations = null;
- try {
- jsonString = downloadJSON(urls[0]);
- try {
- jsonObject = new JSONObject(jsonString);
- jsonArray = new JSONArray(jsonObject.getString("records"));
- stations = createList(jsonArray);
- }
- catch (Exception e) {
- Log.d(DEBUG_JSON, "Erreur de l'objet JSON");
- }
- }
- catch (IOException e) {
- Log.d("doINBckGround", "Impossible de retrouver la page web");
- return stations;
- }
- return stations;
- }
- @Override
- protected void onPostExecute(ArrayList<Station> result) {
- listener.onArrayListReady(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement