E
E
Eserchik2014-07-07 10:04:37
Android
Eserchik, 2014-07-07 10:04:37

How to update LoaderManager from Activity?

Help find a solution.
There is a main Activity with ActionBar and there is a ListFragment in which the list is displayed.
The task is this, when you click on the ActionBar element from the main Activity, update the list in the ListFragment (ie, do lm.getLoader(0).forceLoad()). I don't understand how to do this since the initialization of the loader is done in the fragment.
Main Activity:

//Нажатие на элемент меню ActionBar
  public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
                   
                   //Здесь нужно выполнить запрос на обновление списка 
                     
                   return super.onOptionsItemSelected(item);
      }

//Создаем ListFragment
            FragmentOrdersNew fragment =new FragmentOrdersNew();
      Bundle bundle = new Bundle();
            bundle.putString("orderStatus", "0");
            bundle.putString("tehGuid", tehGuid);
            bundle.putString("tehToken", tehToken);
            fragment.setArguments(bundle);
            fragmentManager.beginTransaction()
                  .replace(R.id.container,fragment)
                  .commit();

And here is the ListFragment:
public class FragmentOrdersNew extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
  final static String LOG_TAG = "myLog";
  private DB db;
  private CustomAdapterOrders adapter;
  private static Cursor c;
  private LoaderManager lm;
  private View rootView;
  private static String orderStatus;
  private String tehGuid;
  private String tehToken;
  



  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);

      
}
  
  
  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);

      
}
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_orders, container, false);

    Bundle bundle = this.getArguments();
    orderStatus = bundle.getString("orderStatus");
    tehGuid = bundle.getString("tehGuid");
    tehToken = bundle.getString("tehToken");


    getData();


    return rootView;
  }



  private void getData(){
    

        db=new DB(getActivity());
        db.open();
        
    c = db.getData(orderStatus);

    if(c!=null && c.getCount()>0){
  
                  lm = getLoaderManager();
      lm.initLoader(0, null, this);
    
    
        String[] from = new String[] {DB.COLUMN_EXT_ID};
        int[] to = new int[] {R.id.tvExId};
        adapter = new CustomAdapterOrders(getActivity(), R.layout.orders_list, null, from, to,lm,db,orderStatus,tehGuid,tehToken);
        setListAdapter(adapter);
  
    }else{
                       TextView tvEmpty=(TextView)rootView.findViewById(android.R.id.empty);
      tvEmpty.setText(R.string.list_empty);
      tvEmpty.setVisibility(View.VISIBLE);


    }

  }



  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // TODO Auto-generated method stub
     return new MyCursorLoader(getActivity(), db);
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
    // TODO Auto-generated method stub


      adapter.swapCursor(c); 


    

  }

  @Override
  public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub
    adapter.swapCursor(null);
  }
  
  
  static class MyCursorLoader extends CursorLoader {

      DB db;
      
      public MyCursorLoader(Context context, DB db) {
        super(context);
        this.db = db;
      }
      
      @Override
      public Cursor loadInBackground() {
      	 c = db.getData(orderStatus);


       return c;
      }
      
    }
  
    public void onDestroy() {
        super.onDestroy();
        db.close();
      }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2014-07-07
@anyd3v

What exactly is the problem? If I understand correctly, then you need to overload onOptionsItemSelected in the fragment and catch the item id you need in it and do the desired action.
If it doesn't work, then it already depends on the perversity that I saw
1. Using FragmentManagera, the desired fragment is searched and onActivityResult is called
2. A message is sent via BroadcastReciever (in the fragment getActivitty().registerReciever(...))

W
wasiliysoft, 2015-06-14
@wasiliysoft

It’s a pity that it’s so late, but nevertheless I’ll offer an answer
Maybe it’s worth moving the initialization of the loader manager to onActivityCreated and, if necessary, updating the data using the loader ’s onContentChanged() method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question