Advertisement
Guest User

Untitled

a guest
Jun 19th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. package com.eu.agendamarinhagrande;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.os.Build;
  8. import android.support.v7.app.ActionBar;
  9. import android.support.v7.app.ActionBarActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ListAdapter;
  17. import android.widget.ListView;
  18. import android.widget.SimpleAdapter;
  19. import android.widget.TextView;
  20.  
  21. import com.eu.agendamarinhagrande.JSONParser;
  22. import com.eu.agendamarinhagrande.R;
  23.  
  24. import org.apache.http.NameValuePair;
  25. import org.json.JSONArray;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;
  32.  
  33.  
  34. public class MainActivity extends ActionBarActivity {
  35.  
  36.     // Progress Dialog
  37.     private ProgressDialog pDialog;
  38.  
  39.     //a variable, you could use from all the methods, that will have as a value the value of the selected TextView
  40.     private String selectedValue;
  41.  
  42.     // Creating JSON Parser object
  43.     JSONParser jParser = new JSONParser();
  44.  
  45.     ArrayList<HashMap<String, String>> empresaList;
  46.  
  47.  
  48.     // url to get all products list
  49.     private static String url_all_empresas = "http://www.grifin.pt/projectoamg/Conexao.php";
  50.  
  51.     // JSON Node names
  52.  
  53.  
  54.     private static final String TAG_TITULO = "Titulo";
  55.  
  56.  
  57.     // products JSONArray
  58.     String resultado = null;
  59.  
  60.     ListView lista;
  61.  
  62.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  63.     @Override
  64.     public void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         setContentView(R.layout.activity_main);
  67.  
  68.         // Hashmap para el ListView
  69.         empresaList = new ArrayList<HashMap<String, String>>();
  70.         LoadAllProducts loadAllProducts = new LoadAllProducts();
  71.         // Cargar los productos en el Background Thread
  72.  
  73.         lista = (ListView) findViewById(R.id.agenda);
  74.         lista.setOnItemClickListener(new OnItemClickListener() {
  75.  
  76.             @Override
  77.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  78.                selectedValue = ((TextView) arg1).getText().toString();
  79.                loadAllProducts.execute(selectedValue);
  80.             }
  81.  
  82.         });
  83.         ActionBar actionBar = getSupportActionBar();
  84.         actionBar.setDisplayHomeAsUpEnabled(true);
  85.  
  86.     }//fin onCreate
  87.  
  88.  
  89.     class LoadAllProducts extends AsyncTask<String, String, String> {
  90.  
  91.         /**
  92.          * Antes de empezar el background thread Show Progress Dialog
  93.          */
  94.         @Override
  95.         protected void onPreExecute() {
  96.             super.onPreExecute();
  97.             pDialog = new ProgressDialog(MainActivity.this);
  98.             pDialog.setMessage("A carregar eventos. Por favor espere...");
  99.             pDialog.setIndeterminate(false);
  100.             pDialog.setCancelable(false);
  101.             pDialog.show();
  102.         }
  103.  
  104.         /**
  105.          * obteniendo todos los productos
  106.          */
  107.         protected String doInBackground(String... args) {
  108.             // Building Parameters
  109.             List params = new ArrayList();
  110.             // getting JSON string from URL
  111.             JSONObject json = jParser.makeHttpRequest(url_all_empresas, "GET", params);
  112.             StringBuilder sb = new StringBuilder();
  113.             // Check your log cat for JSON reponse
  114.             Log.d("All Products: ", url_all_empresas.toString());
  115.            resultado = sb.toString();
  116.             try {
  117.                 // Checking for SUCCESS TAG
  118.  
  119.                 // products found
  120.                 // Getting Array of Products
  121.  
  122.                     JSONArray arrayJson = new JSONArray(resultado);
  123.                 for (int i = 0; i<arrayJson.length();i++){
  124.  
  125.  
  126.  
  127.                 // Storing each json item in variable
  128.                 JSONObject c = arrayJson.getJSONObject(i);
  129.                 String Titulo = c.getString(TAG_TITULO);
  130.  
  131.  
  132.                 // creating new HashMap
  133.                 HashMap map = new HashMap();
  134.  
  135.                 // adding each child node to HashMap key => value
  136.                 map.put(TAG_TITULO, Titulo);
  137.  
  138.  
  139.                 empresaList.add(map);
  140.             }
  141.  
  142.             } catch (JSONException e) {
  143.                 e.printStackTrace();
  144.             }
  145.             return null;
  146.         }
  147.  
  148.         protected void onPostExecute(String file_url) {
  149.             // dismiss the dialog after getting all products
  150.             pDialog.dismiss();
  151.             // updating UI from Background Thread
  152.             runOnUiThread(new Runnable() {
  153.                 public void run() {
  154.                     /**
  155.                      * Updating parsed JSON data into ListView
  156.                      * */
  157.                     ListAdapter adapter = new SimpleAdapter(
  158.                             MainActivity.this,
  159.                             empresaList,
  160.                             R.layout.single_post,
  161.                             new String[]{
  162.                                     TAG_TITULO
  163.  
  164.                             },
  165.                             new int[]{
  166.                                     R.id.single_post_tv_id
  167.  
  168.                             });
  169.                     // updating listview
  170.                     //setListAdapter(adapter);
  171.                     lista.setAdapter(adapter);
  172.                 }
  173.             });
  174.  
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement