0

I want to update ListView of layout activity_call_history.xml contain ListView Id/call_history_listview into a class CallRecordService.java in the Android.

So, I don't know how to initialized ListView to apply to this ListView.

Current, I initialized with code:

listView = new ListView(context);

I search on the Internet, all people usually using like code:

View rootView = inflater.inflate(R.layout.activity_call_history, container, false);
listView = (ListView) rootView.findViewById(R.id.call_history_listview);

But in my class can't create rootView like this code.

How to initialized listView contain R.id.call_history_listview in layout activity_call_history?

This is class CallRecordService.java: http://pastebin.com/6xr72uJ4

Thank you.

king kong
  • 3
  • 3

1 Answers1

1

As in the code provided by you, you are creating to instantiate ListView in your Service class.

public class CallRecordService extends Service {

Any class extending Service class cannot have UI or cannot have access to UI elements. Wherever you've inflated your ListView either in a activity or a fragment, you have to set the adapter in that file. The way to do that is you can send the broadcast from your service after your job is done. The activity/fragment containing your ListView will listen to this broadcast and update the ListView appropriately.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Thank you very much @Shadab Ansari, can you write example code or relate link? I understand your answer but I don't know how to do it. – king kong Mar 17 '16 at 18:15
  • There are several samples available on web. You need to search for "How to communicate between service and activity". You can try these two links also - http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver and https://androidexperinz.wordpress.com/2012/02/14/communication-between-service-and-activity-part-1/ – Shadab Ansari Mar 17 '16 at 18:21
  • Thank you with your answer. – king kong Mar 17 '16 at 18:48