Rss Widget:数据不可显示

时间:2015-06-05 13:55:44

标签: android rss

我使用ShirwaM的[Simplistic-RSS] [1]库将我的Rss-link显示为小部件。我不知道为什么我的项目不起作用。问题是Asynctask工作正常,但结果数据不会显示在ListView中。如果列表视图充满了dummydata,那么一切都很完美。

请看看我的项目。我使用的是Android Studio。
https://www.androidfilehost.com/?fid=23991606952614831
https://drive.google.com/file/d/0BxU5LL-LGpOPTkk2RW1QeGFYME0/view?usp=sharing

一些片段:

RemoteViewsService(.widget.Service.class):

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    int appWidgetId = intent.getIntExtra(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    return (new Provider(this.getApplicationContext(), intent));
}

class Provider(Context applicationContext, Intent intent) {
        context = applicationContext;
        appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
//        new GetRssFeed().execute(RSS_SOURCE_URL);
    }

    @Override
    public RemoteViews getViewAt(int position) {
        Log.i("getViewAt", String.valueOf(position));
        if (position == 0 || position == 1) {
            Log.i("getViewAt", " is working (" + position + ")");
        }
        final RemoteViews remoteView = new RemoteViews(
                context.getPackageName(),
                R.layout.item_awidget);
        remoteView.setTextViewText(R.id.title,"toto");
        remoteView.setTextViewText(R.id.content1, "titi");
        remoteView.setTextViewText(R.id.content2,"titit");
        remoteView.setTextViewText(R.id.content3, "fhjfhjf");
        remoteView.setTextViewText(R.id.content4, "EMPTY");
        return remoteView;
    }
//

    @Override
    public RemoteViews getLoadingView() { return null; }

    @Override
    public void onCreate() {
        //  new GetRssFeed().execute(RSS_SOURCE_URL);
    }

    @Override
    public void onDestroy() { }

    @Override
    public int getCount() { return feeds.size(); }

    @Override
    public int getViewTypeCount() { return 1; }

    @Override
    public long getItemId(int position) { return position; }

    @Override
    public boolean hasStableIds() { return true; }

    @Override
    public void onDataSetChanged() {
        //   new GetRssFeed().execute(RSS_SOURCE_URL);
    }


AsyncTask(to bring in):

    public class GetRssFeed extends AsyncTask<String, Void, List<RssItem>> {
        @Override
        protected List<RssItem> doInBackground(String... params) {
            try {
                RssReader rssReader = new RssReader(params[0]);
                Log.i("dpInBack ", String.valueOf(rssReader.getItems().size()));
                return rssReader.getItems();
            } catch (Exception e) {
                Log.v("Error Parsing Data", e + "");
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<RssItem> pList ) {
            Log.i("list size ", String.valueOf(pList.size()));
            for(int i=0; i<pList.size(); i++) {
                if (i == 0) {
                    Log.i("onPostExecute", pList.get(i).getTitle());
                }
                RssItem item = new RssItem();
                item.setTitle(pList.get(i).getTitle());
                item.setDescription(pList.get(i).getDescription());
                item.setLink(pList.get(i).getLink());
                item.setImageUrl(pList.get(i).getImageUrl());
                feeds.add(item);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

哦,我终于找到了解决方法。诀窍是使用AsyncTask来使用AsyncTask。在这种情况下不可能使用@Override public void onCreate() { new GetRssFeed().execute(RSS_SOURCE_URL); } @Override public void onDataSetChanged() { new GetRssFeed().execute(RSS_SOURCE_URL); } @Override public void onDestroy() { } public class GetRssFeed extends AsyncTask<String, Void, List<RssItem>> { @Override protected List<RssItem> doInBackground(String... params) { [..] } @Override protected void onPostExecute(List<RssItem> pList ) { [..] } }

例如,更改..

@Override
public void onCreate() {
    feeds.clear;
    initData();
}

@Override
public void onDataSetChanged() {
    feeds.clear;
    initData();
}

@Override
public void onDestroy() {
    feeds.clear;
}

public initData(){ [..] }


要..

In [18]:
BS2(100.,100.,1.,.001,.3)

Out[18]:
11.96762435837207