I create a ListView
in my widget and getting the result from a web service. I don't have any problem in getting the result from the web service but the result is not displaying on my ListView
and even updatePeriodMillis
that updates the widget
every 30 minutes is not working.
I'm following this example and added AsyncTask
in the ListProvider
class, I managed to get the result and instead of using the populateListItem()
where it is like this
private void populateListItem() {
for (int i = 0; i < 10; i++) {
ListItem listItem = new ListItem();
listItem.heading = "Heading" + i;
listItem.content = i
+ " This is the content of the app widget listview.";
listItemList.add(listItem);
}
}
I use the code below in my AsyncTask
on onPostExecute
public class ListProvider implements RemoteViewsFactory {
private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
public ListProvider(Context context, Intent intent) {
new GetJSONWebService().execute();
}
class GetJSONWebService extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL_STRING);
HttpResponse response = httpclient.execute(httpget);
str = EntityUtils.toString(response.getEntity());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject object = new JSONObject(result);
JSONArray jArray = object.getJSONArray("DATA");
for (int i = 0, count = jArray.length(); i < count; i++) {
try {
JSONObject jsonObject = jArray.getJSONObject(i);
ListItem listItem = new ListItem();
listItem.heading = jsonObject.getString("name").toString();
listItem.content = jsonObject.getString("description").toString();
listItemList.add(listItem);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When debugging, it adds the items on my listItemList
but it doesn't display on my widget ListView
. How can I solve this? Thank you in advance.
Here is my WidgetService
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new ListProvider(this.getApplicationContext(), intent));
}
}
Here is my AppWidgetProvider
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}