Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How call onCreateView Method or refresh my fragment?

I would like to refresh or call onCreateView in the code following . I have a contex menù to delete an Item and after I would like to refresh the fragment with the new Item.. Thanks at all!

 public class ItemDetailFragmentBlackBoard extends Fragment {
       
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle  savedInstanceState) {
                ....
               return rootView;
         }
      

     /** Menu on LongClick */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)  {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu");
        menu.add(0, v.getId(), 0, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="Delete"){
            String status="";
            AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
            int posizione = info.position;
            String[] messaggioDaCancellare= S.getMessaggiInfo().get(posizione); 
            try{
                JSONObject del =ProxyUtils.proxyCall("deleteMessage",messaggioDaCancellare[4]);
                status=del.getString("status");
            } catch (Exception e) {
                Log.i("Eccezione", e.toString());
            }
            Activity activity= getActivity();
            if(status.equals("OK")){
                
                       **HERE......I would like to refresh my fragment o recall onCreateView method...**
                                
                Toast.makeText(activity, "Delete avvenuta", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(activity, "Delete non riuscita", Toast.LENGTH_SHORT).show();
            }
        } else {return false;}  
        return true;  
    }
like image 849
alfo888_ibg Avatar asked Jan 22 '13 17:01

alfo888_ibg


1 Answers

It's better to define a viewGroup like a simple linearLayout (call it screen) in onCreateView function, and fill it in a function like init(). Each time you want to re-create your view, just remove all linearLayout's children and call init().

You did good job but it's more expensive than my simple solution.

like image 109
Afshin Avatar answered Sep 28 '22 07:09

Afshin