king kong
king kong

Reputation: 3

How to initialized ListView of a layout?

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.

Upvotes: 0

Views: 42

Answers (1)

Shadab Ansari
Shadab Ansari

Reputation: 7070

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.

Upvotes: 1

Related Questions