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

2023-09-08移动开发问题
26

本文介绍了Widget 中 ListView 上的项目不显示 [Android]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

我正在关注 这个例子并添加了 AsyncTaskListProvider 类中,我设法得到了结果,而不是像这样使用 populateListItem()

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

我在 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 上.我该如何解决这个问题?先感谢您.

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.

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

推荐答案

我按照 答案在这里 我发现我的 getCount 总是返回 0,因此我没有使用 AsyncTask 我把我的网络服务调用放在 onDataSetChanged().

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().

这篇关于Widget 中 ListView 上的项目不显示 [Android]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

CCScrollView 滚动和触摸事件永远不会触发
CCScrollView scroll and touch events never firing(CCScrollView 滚动和触摸事件永远不会触发)...
2024-08-12 移动开发问题
1

使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘
removing jagged edges of my ropes using antialiasing of OpenGLES(使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘)...
2024-08-12 移动开发问题
34

cocos2d 在场景之间移动
cocos2d Moving between scene(cocos2d 在场景之间移动)...
2024-08-12 移动开发问题
2