Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.eu.agendamarinhagrande;
- import android.annotation.TargetApi;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.support.v7.app.ActionBar;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import com.eu.agendamarinhagrande.JSONParser;
- import com.eu.agendamarinhagrande.R;
- import org.apache.http.NameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class MainActivity extends ActionBarActivity {
- // Progress Dialog
- private ProgressDialog pDialog;
- //a variable, you could use from all the methods, that will have as a value the value of the selected TextView
- private String selectedValue;
- // Creating JSON Parser object
- JSONParser jParser = new JSONParser();
- ArrayList<HashMap<String, String>> empresaList;
- // url to get all products list
- private static String url_all_empresas = "http://www.grifin.pt/projectoamg/Conexao.php";
- // JSON Node names
- private static final String TAG_TITULO = "Titulo";
- // products JSONArray
- String resultado = null;
- ListView lista;
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Hashmap para el ListView
- empresaList = new ArrayList<HashMap<String, String>>();
- LoadAllProducts loadAllProducts = new LoadAllProducts();
- // Cargar los productos en el Background Thread
- lista = (ListView) findViewById(R.id.agenda);
- lista.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- selectedValue = ((TextView) arg1).getText().toString();
- loadAllProducts.execute(selectedValue);
- }
- });
- ActionBar actionBar = getSupportActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
- }//fin onCreate
- class LoadAllProducts extends AsyncTask<String, String, String> {
- /**
- * Antes de empezar el background thread Show Progress Dialog
- */
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setMessage("A carregar eventos. Por favor espere...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(false);
- pDialog.show();
- }
- /**
- * obteniendo todos los productos
- */
- protected String doInBackground(String... args) {
- // Building Parameters
- List params = new ArrayList();
- // getting JSON string from URL
- JSONObject json = jParser.makeHttpRequest(url_all_empresas, "GET", params);
- StringBuilder sb = new StringBuilder();
- // Check your log cat for JSON reponse
- Log.d("All Products: ", url_all_empresas.toString());
- resultado = sb.toString();
- try {
- // Checking for SUCCESS TAG
- // products found
- // Getting Array of Products
- JSONArray arrayJson = new JSONArray(resultado);
- for (int i = 0; i<arrayJson.length();i++){
- // Storing each json item in variable
- JSONObject c = arrayJson.getJSONObject(i);
- String Titulo = c.getString(TAG_TITULO);
- // creating new HashMap
- HashMap map = new HashMap();
- // adding each child node to HashMap key => value
- map.put(TAG_TITULO, Titulo);
- empresaList.add(map);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(String file_url) {
- // dismiss the dialog after getting all products
- pDialog.dismiss();
- // updating UI from Background Thread
- runOnUiThread(new Runnable() {
- public void run() {
- /**
- * Updating parsed JSON data into ListView
- * */
- ListAdapter adapter = new SimpleAdapter(
- MainActivity.this,
- empresaList,
- R.layout.single_post,
- new String[]{
- TAG_TITULO
- },
- new int[]{
- R.id.single_post_tv_id
- });
- // updating listview
- //setListAdapter(adapter);
- lista.setAdapter(adapter);
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement