Pass ActionbarActivity to Fragments

0

In order to use an activity in navigation drawer a activity needs ta extend fragments, and my ta extend action bar And in my action bar, have the codes of a remote database, how do I get the action bar for fragments? I created another class and passed the data to it with extend fragments, I made changes like getView().findviewbyId etc, but onPreExecute() is giving error. And posting too. Follow below.

OnPreExecute

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Cid_Itaborai_Fragment.this);
        pDialog.setMessage("Carregando anuncios...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

OnPostExecute

protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        Cid_Itaborai_Fragment.this,
                        empresaList,
                        R.layout.formato_postagem,
                        new String[] {
                                TAG_ID,
                                TAG_NOMBRE,
                                TAG_PRECO,
                                TAG_DESCRIPTION,

                        },
                        new int[] {
                                R.id.single_post_tv_id,
                                R.id.single_post_tv_nombre,
                                R.id.single_post_tv_preco,
                                R.id.single_post_tv_description,
                        });
                // updating
                //setListAdapter(adapter);
                lista.setAdapter(adapter);
            }
        });
    }
}

Below is the full fragment code.

public class Cid_Itaborai_Fragment extends Fragment {

public Cid_Itaborai_Fragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// Hashmap para el ListView
    empresaList = new ArrayList<HashMap<String, String>>();

    // Cargar los productos en el Background Thread
    new LoadAllProducts().execute();
    lista = (ListView) getView().findViewById(R.id.listAllProducts);
}
// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> empresaList;


// Url do site
private static String url_all_empresas = "http://yeloposter.esy.es/Itaborai/Cid_Itaborai/get_all_empresas.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "empresas";
private static final String TAG_ID = "id";
private static final String TAG_NOMBRE = "nombre";
private static final String TAG_PRECO = "preco";
private static final String TAG_DESCRIPTION = "description";



// products JSONArray
JSONArray products = null;

ListView lista;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Do something that differs the Activity's menu here
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_login:

            // Not implemented here
            return false;
        case R.id.action_Configuracoes:
            // Do Fragment menu item stuff here
            return true;
        default:
            break;
    }

    return false;
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}

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(Cid_Itaborai_Fragment.this);
        pDialog.setMessage("Carregando anuncios...");
        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);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                //Log.i("ramiro", "produtos.length" + products.length());
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NOMBRE);
                    String preco = c.getString(TAG_PRECO);
                    String description = c.getString(TAG_DESCRIPTION);


                    // creating new HashMap
                    HashMap map = new HashMap();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NOMBRE, name);
                    map.put(TAG_PRECO, preco);
                    map.put(TAG_DESCRIPTION, description);


                    empresaList.add(map);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        Cid_Itaborai_Fragment.this,
                        empresaList,
                        R.layout.formato_postagem,
                        new String[] {
                                TAG_ID,
                                TAG_NOMBRE,
                                TAG_PRECO,
                                TAG_DESCRIPTION,

                        },
                        new int[] {
                                R.id.single_post_tv_id,
                                R.id.single_post_tv_nombre,
                                R.id.single_post_tv_preco,
                                R.id.single_post_tv_description,
                        });
                // updating
                //setListAdapter(adapter);
                lista.setAdapter(adapter);
            }
        });
    }
}

}

    
asked by anonymous 30.08.2015 / 19:22

0 answers