【问题标题】:How to show textviews in widget and display a next button?如何在小部件中显示文本视图并显示下一个按钮?
【发布时间】:2011-09-15 12:51:59
【问题描述】:

我有一个从 html 检索到的项目列表。

都是头衔。

我正在做的是针对特定事物测试每个标题..

如果有,我想将它添加到小部件中的 TextView。

唯一的问题是,如果它们超过 1 个项目,小部件将无法容纳所有项目。

3.0 之前的小部件不支持列表视图布局。

那么如何在 TextView 上添加每个项目,然后使用下一个按钮显示每个项目以显示下一个 textview?

编辑:

好的,听起来很有道理。 所以在这里我如何检索我的标题...

关于将它们设置为 ArrayList 并在单击 textview 时显示列表中的下一项,您有什么建议?

while(doc == null && retry < 5){
                            retry++;
                            try {
                            doc = Jsoup.connect(site).get();
                        } catch (IOException e) {
                            Log.e("Html", "JSoup get retrieved no document", e);
                        }


                            }

                        if(doc != null){

                             title = doc.select("tr>  td.indexList1, tr > td.indexList2");
                            if(title != null){
                                // Iterator over those elements     
                            ListIterator<Element> postIt = title.listIterator();   


                            //Loads all the items until there is no .hasNExt()
                            while (postIt.hasNext()) {     



                                    // Add the game text to the ArrayList     
                                    Element name = postIt.next();
                                     nameString = name.text();


                                    list.add(new Release(nameString));

那么我将如何使用它最初设置一个文本视图,然后(使用您建议的代码)允许将列表中的下一项加载到文本视图中?

【问题讨论】:

  • "3.0 之前的小部件不支持列表视图布局。"现在呢?

标签: android android-widget


【解决方案1】:

将标题保留在列表中(java 列表,例如 ArrayList),并且布局中只定义了 1 个文本视图。

在您的清单中,为您的小部件提供者(接收者)添加一个新的意图过滤器,例如:

<intent-filter>
        <action android:name="WIDGET_NEXT_TITLE" />
    </intent-filter>

并在您的小部件提供程序类中添加一个 onClickPendingIntent 到您的文本视图:

final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);                
            final Intent nextTitleIntent = new Intent("WIDGET_NEXT_TITLE");
            nextTitleIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, nextTitleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.textview_id, pendingIntent);

例如,您可以在 onUpdate 方法中执行此操作。 现在,每次点击都会触发意图,您需要做的就是抓住它并在文本视图中设置下一个标题。在你的 widgetprovider 覆盖方法 onRecieve 像这样:

if ("WIDGET_NEXT_TITLE".equals(intent.getAction())) {
        final int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            // get the next title here and set the text in a textview through RemoteView
        }
    } else {
        super.onReceive(context, intent);
    }

类似的东西。当然它非常简化,因为你需要跟踪当前显示的标题,但我希望你明白。

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 2018-09-16
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多