2

In my Fragment class, this is how I add an Item to my RecyclerView.

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

            recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setHasFixedSize(true);

            theDataList = new ArrayList<>();
            theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(theRecyclerAdpater);

            TheData a = new TheData("01","another value","another value","another value","another value");
                        theDataList.add(a);
                    a = new TheData("02","another value","another value","another value","another value");
                        theDataList.add(a);
            theRecyclerAdpater.notifyDataSetChanged();

            return view;
        }

But what if I want to add an Item to my RecyclerView via Service when the UI is Available? I mean, The Service is running in background and if the Main UI of application where the RecyclerView is Available then Add an Item Otherwise don't Add an Item. Anyone has an idea on how can i possible do that?

For now, I only have this in my Service

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        //Add an Item here to RecyclerView if the UI is Available.
    }

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

UPDATE I just try this but not working.

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

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        theDataList = new ArrayList<>();
        theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(theRecyclerAdpater);

        getActivity().startService(new Intent(getActivity(),MyService.class));
        bus.register(this);

        return view;
     }

            @Subscribe
            public void getMessage(String s) {
               TheData a = new TheData(s);
               theDataList.add(a);
               theRecyclerAdpater.notifyDataSetChanged();
           }

in my Service

@Override
public void onCreate(){
     bus.post("PP");
}
  • use `LocalBroadcastReceiver` – Gaurav Jun 30 '16 at 06:14
  • 1
    Possible duplicate of [Communication between Activity and Service](http://stackoverflow.com/questions/20594936/communication-between-activity-and-service) – Jin Jun 30 '16 at 06:51

3 Answers3

1

First thing first, Otto is now depreciated, I had to change it to EventBus. I had to ask it again, Thank to you. So this is what I did.

added to gradle compile 'org.greenrobot:eventbus:3.0.0'

In my Fragment Class

/*I had to import*/
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public static class MyFragment extends Fragment {

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    static TheRecyclerAdapter theRecyclerAdpater;
    private List<TheData> theDataList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this); //Register it first

        View view = inflater.inflate(R.layout.online_devices, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        theDataList = new ArrayList<>();
        theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(theRecyclerAdpater);

        getActivity().startService(new Intent(getActivity(),MyService.class)); //I call the Service

        return view;
    }

    //Subscribe, run it on UI
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(TheData data){
        TheData a = new TheData(data.getPc_number());
        theDataList.add(a);
        theRecyclerAdpater.notifyDataSetChanged();
    }
}

and in my Service class

public class ServerHelperService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
       EventBus.getDefault().post(new TheData("OK")); //and This
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

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

and it's done. Still reading on it's guide. Thanks for the help!

Community
  • 1
  • 1
0

You have many options, but the most interesting one is to update recyclerView via EventBus (for example Otto:

Write this inside your app gradle file.

dependencies {
  compile 'com.squareup:otto:1.3.8'
}

declare it inside your application (Use dagger2 to avoid static)

public static Bus bus = new Bus(ThreadEnforcer.MAIN); 

then create a receiving method inside your activity(where recycler is declared) and register it in Oncreate method of your activity:

@Subscribe
public void getMessage(String s) {
  adapter.addItem(s); // suppose adapter is recyclerAdapter
}

//requires a registration e.g. in the onCreate method
//bus is your static variable declared in Application class.
bus.register(this); 

Then inside your onCreate method of Service call:

@Override
public void onCreate(){
    bus.post("Hello");
}
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
0

This question is poorly phrased. What you are really asking is how you can communicate data from your service to your activity/fragment (presumably loading some IO intensive data or something), and then the activity/fragment can update the adapter and the UI accordingly.

There are plenty of other SO questions that answer this, for example:

How to have Android Service communicate with Activity

Communication between Activity and Service

Community
  • 1
  • 1
Jin
  • 6,055
  • 2
  • 39
  • 72