Advertisement
ziedrebhi

ListDataActivity.java

Apr 19th, 2014
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package com.mysql.enisandroidclub;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9.  
  10. import android.app.Activity;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.ListView;
  16. import android.widget.Toast;
  17.  
  18. public class ListDataActivity extends Activity {
  19.     CustomProgressDialog  progressDialog;
  20.     String urlGet="http://192.168.1.5/enis_android_club/affichage_bd.php";
  21.     GetDataAsyncTask getData;
  22.     String message;
  23.     int success;
  24.     ListView lv;
  25.     List<String> myListofData ;
  26.     ArrayAdapter arrayadp;
  27.    
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_list_data);
  32.         progressDialog = new CustomProgressDialog(this, R.drawable.loading_throbber);
  33.         progressDialog.setCancelable(true);
  34.         lv=(ListView)findViewById(R.id.listView1);
  35.         myListofData = new ArrayList<String>();
  36.         getData=new GetDataAsyncTask();
  37.         getData.execute();   
  38.     }
  39.  
  40.    
  41.     private class GetDataAsyncTask extends  AsyncTask<Void, Void, Void> {
  42.         @Override
  43.         protected void onPreExecute() {
  44.             Log.i("add", "onPreExecute");
  45.             super.onPreExecute();
  46.             progressDialog.show();
  47.         }
  48.        
  49.         @Override
  50.         protected Void doInBackground(Void... params) {
  51.             Log.i("add", " start doInBackground");
  52.             ServiceHandler sh = new ServiceHandler();
  53.            
  54.             // Making a request to url and getting response
  55.         String jsonStr = sh.makeServiceCall(urlGet, ServiceHandler.GET);
  56.  
  57.         Log.d("Response: ",jsonStr);
  58.            
  59.         if (jsonStr != null) {
  60.         try {
  61.             JSONObject jsonObj = new JSONObject(jsonStr);
  62.             // return value of success
  63.             success=jsonObj.getInt("success");
  64.             Log.i("success", String.valueOf(success));
  65.             if (success==0)
  66.             {
  67.                 // success=0 ==> there is a string = message
  68.                 message=jsonObj.getString("message");
  69.                 Log.i("message", message);
  70.             }
  71.             else if (success==1)
  72.             {
  73.                 // success=1 ==> there is an array of data = valeurs
  74.                 JSONArray dataValues = jsonObj.getJSONArray("valeurs");
  75.                 // loop each row in the array
  76.                 for(int j=0;j<dataValues.length();j++)
  77.                 {
  78.                     JSONObject values = dataValues.getJSONObject(j);
  79.                     // return values of col1 in valCol1
  80.                     String valCol1= values.getString("col1");
  81.                     // return values of col2 in valCol2
  82.                     String valCol2= values.getString("col2");
  83.                     String valCol3= values.getString("col3");
  84.                     String valCol4= values.getString("col4");
  85.                     //add a string witch contains all of data getted from the response
  86.                     myListofData.add(valCol1+" - "+valCol2+" - "+valCol3+" - "+valCol4);
  87.                     Log.i("Row "+(j+1), valCol1+" - "+valCol2+" - "+valCol3+" - "+valCol4);
  88.                 }
  89.             }
  90.                      
  91.         } catch (JSONException e) {
  92.             e.printStackTrace();
  93.         }
  94.         } else {
  95.             Log.e("ServiceHandler", "Couldn't get any data from the url");
  96.         }
  97.  
  98.         Log.i("add", " end doInBackground");
  99.         return null;
  100.         }
  101.        
  102.         @Override
  103.         protected void onPostExecute(Void result) {
  104.             Log.i("add", "onPostExecute");
  105.             super.onPostExecute(result);
  106.             if (progressDialog.isShowing())
  107.             {
  108.                 progressDialog.dismiss();
  109.             }
  110.             if(success==1)
  111.             {
  112.                 Toast.makeText(getApplicationContext(), "Bien récues ", Toast.LENGTH_LONG).show();
  113.                 // show the list view contains the data
  114.                 arrayadp=new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1, myListofData);                                    
  115.                 lv.setAdapter(arrayadp);  
  116.             }
  117.             else
  118.             {
  119.                 Toast.makeText(getApplicationContext(), "Erreur", Toast.LENGTH_LONG).show();
  120.             }
  121.      
  122.      
  123.         }
  124.        
  125.     }
  126.    
  127.  
  128.    
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement