Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. this.view = inflater.inflate(R.layout.fragment_list, container, false);
  5.  
  6. // create instance of my async task class
  7. lp = new RestfulConnect(getActivity());
  8. // set onArrayListReady as a listener.
  9. lp.setOnListReady(this);
  10. // do the
  11. lp.getStationsArray(Integer.toString(this.start));
  12. return view;
  13. }
  14. @Override
  15. public void onArrayListReady(ArrayList<Station> stations) {
  16.  
  17. adapter = new StationListAdapter(getActivity().getApplicationContext(), R.layout.fragment_list_item, stations);
  18. listView = (ListView) this.view.findViewById(R.id.fragment_list_stations_list);
  19. listView.setAdapter(adapter);
  20.  
  21. // Listener lie a l'action de click sur un objet de la liste
  22. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  23.  
  24. @Override
  25. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  26. Station station = (Station) listView.getAdapter().getItem(i);
  27. Intent intent = new Intent(getActivity().getApplicationContext(), Detail.class);
  28. intent.putExtra("station", station);
  29. startActivity(intent);
  30. }
  31. });
  32.  
  33. // Listener qui permet de charger les donnees suivantes
  34. listView.setOnScrollListener(new OnScrollListener() {
  35.  
  36. @Override
  37. public void onScrollStateChanged(AbsListView view,
  38. int scrollState) { // TODO Auto-generated method stub
  39. int threshold = 1;
  40. int count = listView.getCount();
  41.  
  42. if (scrollState == SCROLL_STATE_IDLE) {
  43. if (listView.getLastVisiblePosition() >= count - threshold) {
  44. // WHERE I'D LIKE TO EXECUTE THE PREVIOUS INSTANCE OF LP
  45. int start = 20;
  46. this.lp.getStationsArray(Integer.toString(start));
  47. }
  48. }
  49. }
  50.  
  51. @Override
  52. public void onScroll(AbsListView view, int firstVisibleItem,
  53. int visibleItemCount, int totalItemCount) {
  54. // TODO Auto-generated method stub
  55.  
  56. }
  57.  
  58. });
  59. }
  60.  
  61. public class RestfulConnect {
  62.  
  63.  
  64. // Constructor
  65. public RestfulConnect(Context context) {
  66. this.context = context;
  67. }
  68.  
  69. public void getStationsArray(String... filters) {
  70.  
  71. String apiUrl = requestUrlBuilder(filters);
  72. ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  73. NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  74. if(networkInfo != null && networkInfo.isConnected()) {
  75. new getStationsAsync().execute(apiUrl);
  76. }
  77. else {
  78. Log.d(DEBUG_MAIN_CALL, "Pas de connexion disponible");
  79. }
  80. }
  81.  
  82. private class getStationsAsync extends AsyncTask<String, Void, ArrayList<Station>> {
  83.  
  84. @Override
  85. protected ArrayList<Station> doInBackground(String... urls) {
  86.  
  87. String jsonString;
  88. JSONObject jsonObject;
  89. JSONArray jsonArray;
  90. ArrayList<Station> stations = null;
  91.  
  92. try {
  93. jsonString = downloadJSON(urls[0]);
  94. try {
  95. jsonObject = new JSONObject(jsonString);
  96. jsonArray = new JSONArray(jsonObject.getString("records"));
  97. stations = createList(jsonArray);
  98. }
  99. catch (Exception e) {
  100. Log.d(DEBUG_JSON, "Erreur de l'objet JSON");
  101. }
  102. }
  103. catch (IOException e) {
  104. Log.d("doINBckGround", "Impossible de retrouver la page web");
  105. return stations;
  106. }
  107. return stations;
  108. }
  109.  
  110. @Override
  111. protected void onPostExecute(ArrayList<Station> result) {
  112. listener.onArrayListReady(result);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement