【问题标题】:Items on ListView in Widget doesn't display [Android]Widget 中 ListView 上的项目不显示 [Android]
【发布时间】:2016-09-01 08:15:47
【问题描述】:

我在我的小部件中创建了一个ListView 并从网络服务中获取结果。我从 Web 服务获取结果没有任何问题,但结果没有显示在我的 ListView 上,甚至 updatePeriodMillis 每 30 分钟更新一次 widget 也不起作用。

我关注this example 并在ListProvider 类中添加了AsyncTask,我设法得到了结果,而不是像这样使用populateListItem()

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

我在onPostExecute 上的AsyncTask 中使用下面的代码

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

【问题讨论】:

    标签: android listview widget


    【解决方案1】:

    你添加adapter.notifyDataSetChanged()了吗?

    【讨论】:

    • 我没用过。我应该把它放在哪里以及它是如何工作的?
    • 应该在将内容添加到列表后调用它。例如:在 populateListItem() 中将其添加到 for 循环之后。
    • 我应该输入listItemList.notifyDataSetChanged() 吗?在哪里ArrayList&lt;ListItem&gt; listItemList = new ArrayList&lt;ListItem&gt;(); 对不起,我很困惑。
    • 没有。如果您使用 ListView,则必须为其设置适配器。你使用适配器来显示你的列表内容吗?
    • 我的Adapter 是我的ListProvider
    【解决方案2】:

    我通过关注the answer here 解决了这个问题,我发现我的getCount 总是返回0,因此我没有使用AsyncTask,而是将我的网络服务调用放在onDataSetChanged() 中。

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      相关资源
      最近更新 更多