Items on ListView in Widget doesn't display [Android]
Asked Answered
Y

2

0

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;
    }
Yowl answered 1/9, 2016 at 8:15 Comment(0)
Y
1

I solve this by following the answer here I found out that my getCount always returns 0 and therefore instead of using AsyncTask I put my web service call in onDataSetChanged().

Yowl answered 2/9, 2016 at 3:0 Comment(0)
J
1

did you added adapter.notifyDataSetChanged()?

Jacobo answered 1/9, 2016 at 8:18 Comment(9)
I haven't use it. Where should I put it and how it works?Yowl
it shoud be called after you add content to your list. for example: in populateListItem() add it after the for loop.Jacobo
Should I put listItemList.notifyDataSetChanged() ? Where ArrayList<ListItem> listItemList = new ArrayList<ListItem>(); Sorry I'm confuse.Yowl
no. if you use ListView, so you must set adapter to it. do you use adapter to show your list content?Jacobo
My Adapter is my ListProviderYowl
so try to call notifyDataSetChanged() from your ListProvider instance: provider.notifyDataSetChanged();Jacobo
It only gives me notify() and notifyAll() I tried doing like this listProvider = new ListProvider(this.getApplicationContext(), intent); listProvider.notifyAll();Yowl
try listItemList.setRemoteViewsAdapter(intent)Jacobo
i don't know if i can help you but take a look here vogella.com/tutorials/AndroidWidgets/article.htmlJacobo
Y
1

I solve this by following the answer here I found out that my getCount always returns 0 and therefore instead of using AsyncTask I put my web service call in onDataSetChanged().

Yowl answered 2/9, 2016 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.