私のFragment
クラスでは、これが私のRecyclerView
にアイテムを追加する方法です。サービスクラスを使用して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;
}
しかし、私はService
を経由して私のRecyclerView
に項目を追加したい場合はUI
はありますか?つまり、Service
はバックグラウンドで実行されており、RecyclerView
がAvailable
の場合、Main UI
のアプリケーションの場合は、アイテムを追加します。それ以外の場合はアイテムを追加しません。誰でもどのように私はそれを行うことができますか?今の
、私は、私はちょうどこれをしようとしたが動作していない私の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 でこれを持っています。私のサービスで
@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();
}
@Override
public void onCreate(){
bus.post("PP");
}
'LocalBroadcastReceiver'を使用 – Gaurav
[アクティビティとサービス間の通信]の複製が可能ですか?(http://stackoverflow.com/questions/20594936/communication-between-activity-and-service) – Jin