Pass Actionbaractivity to Fragments

Asked

Viewed 59 times

0

For me to use an acitivity in navigation Rawer a Activity precisa ta extend Fragments, e a minha ta extend action bar And in this action bar, I have the codes of a remote database, how do I put the action bar for Ragments? I created another class and passed the data to her with extend Fragments, I made some changes as in getView().findviewbyId etc., however onPreExecute() ta giving error. And in the post too. Follows below.

Preexecute

 @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);
            }
        });
    }
}

Follow the full Fragment code below.

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);
            }
        });
    }
}

}

  • I think you’re confusing things a little bit, or I didn’t understand, because you say how to put Activity in the navigation Drawer and put the code of a class AsyncTask, The android Studio or Eclipse has an option when creating a new project, and you can choose which type of Activitymain initial you want for your application, la you can choose an Activity with navigation Drawer, ai you study the code.

  • @Skywalker is that, I have ready the navigationdrawer ready, but before that was just with action bar . And now that I’ve set the navigation I’m having trouble passing the old code to switch to fragments and function properly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.