5

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;  
    }
ankalagba
  • 94
  • 1
  • 8
alfo888_ibg
  • 1,847
  • 5
  • 25
  • 43

3 Answers3

4

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.

Afshin
  • 977
  • 9
  • 16
2

I solved my problem replecing my fragmet with itself

the code is :

{

    arguments.putString(ItemDetailFragmentBlackBoard.ARG_ITEM_ID, id);
    ItemDetailFragmentBlackBoard fragment= new ItemDetailFragmentBlackBoard();
    fragment.setArguments(arguments);
    getFragmentManager().beginTransaction().replace(R.id.item_detail_container,   fragment).commit();   
}

ItemDetailFragmentBlackBoard is my fragment. I am deleting an Item from listView and after I delete it i re-call my fragment with de code above so I get un refresh!

gprathour
  • 14,813
  • 5
  • 66
  • 90
alfo888_ibg
  • 1,847
  • 5
  • 25
  • 43
0
public class ItemDetailFragmentBlackBoard extends Fragment {
    public static View _rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (_rootView == null || _isRefreshDashboard) {
        _rootView = inflater.inflate(R.layout.ItemDetailFragmentBlackBoard, container, false);

        // your code can't be change without refreshFragment in here..

        _isDashboardRefresh = false;
    }
}

// Global variables

public abstract class CommonBase extends AppCompatActivity {
    public static boolean _isRefreshDashboard;
}

// Refresh your fragment

CommonBase._isRefreshDashboard = true;
Ihdina
  • 950
  • 6
  • 21