Widget中ListView上的项目不显示[Android]

时间:2016-09-01 08:15:47

标签: android listview widget

我在我的小部件中创建了一个ListView并从Web服务获取结果。我从Web服务获取结果时没有任何问题,但结果未显示在ListView甚至updatePeriodMillis上,每30分钟更新一次widget无法正常工作。

我正在关注this example并在AsyncTask课程中添加了ListProvider,我设法获得了结果,而不是使用populateListItem(),就像这样< / p>

 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);
    }
}

我在AsyncTask

上的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();
        }
 }

调试时,它会在listItemList上添加项目,但不会显示在我的小部件ListView上。我怎么解决这个问题?先感谢您。

这是我的WidgetService

public class WidgetService extends RemoteViewsService {

 @Override
  public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return (new ListProvider(this.getApplicationContext(), intent));
  }
}

这是我的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;
    }

2 个答案:

答案 0 :(得分:1)

你添加了adapter.notifyDataSetChanged()吗?

答案 1 :(得分:1)

我按照the answer here解决了这个问题我发现我的getCount总是返回0而不是使用AsyncTask我将我的网络服务调用放在onDataSetChanged()中。