【问题标题】:How to load items in android homescreen listview widget?如何在 android 主屏幕列表视图小部件中加载项目?
【发布时间】:2014-01-27 11:40:49
【问题描述】:

我目前正在开发一个使用主屏幕小部件向用户显示数据的 Android 项目。我在 Widget 中使用的视图是 ListView。以下是我的代码:

WidgetService 类

public class WidgetService extends RemoteViewsService
{

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

}

WidgetProvider 类

public class WidgetProvider extends AppWidgetProvider
{   
    @Override
    public void onDeleted(Context context, int[] appWidgetIds)
    {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) 
    {
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) 
    {
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) 
    {
        for(int i=0;i<appWidgetIds.length;i++)
        {
            RemoteViews rv = new RemoteViews(context.getPackageName(),
                    R.layout.widget);

            Intent intent = new Intent(context, WidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetIds[i]);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

            rv.setRemoteAdapter(R.id.widgetListView, intent);

            appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

WidgetRemoteViewsFactory 类

public class WidgetRemoteViewsFactory implements RemoteViewsFactory
{
private Context context = null;
private int appWidgetId;

private List<String> widgetList = new ArrayList<String>();
private DBHelper dbhelper;

public WidgetRemoteViewsFactory(Context context, Intent intent)
{
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    Log.d("AppWidgetId", String.valueOf(appWidgetId));
    dbhelper = new DBHelper(this.context);
}

private void updateWidgetListView()
{
    String[] widgetFruitsArray = dbhelper.retrieveFruitsList();
    List<String> convertedToList = new ArrayList<String>(Arrays.asList(widgetFruitsArray));
    this.widgetList = convertedToList;
}

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

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

@Override
public RemoteViews getLoadingView()
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public RemoteViews getViewAt(int position)
{
    Log.d("WidgetCreatingView", "WidgetCreatingView");
    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
            R.layout.listview_row_item);

    Log.d("Loading", widgetList.get(position));
    remoteView.setTextViewText(R.id.listTV, widgetList.get(position));

    return remoteView;
}

@Override
public int getViewTypeCount()
{
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean hasStableIds()
{
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onCreate()
{
    // TODO Auto-generated method stub
    updateWidgetListView();
}

@Override
public void onDataSetChanged()
{
    // TODO Auto-generated method stub
    updateWidgetListView();
}

@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    widgetList.clear();
    dbhelper.close();
}
}

Android 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listviewwithdb"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme.Light" >

    <activity
        android:name="com.example.listviewwithdb.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.example.listviewwithdb.WidgetProvider" >
        <intent-filter >
            <action 
                android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widgetinfo" />
    </receiver>

    <service
        android:name="com.example.listviewwithdb.WidgetService"
        android:exported="false"
        android:permission="android.permission.BIND_REMOTEVIEWS" />

</application>

</manifest>

问题是我可以将小部件添加到主屏幕中,但它会在列表视图中显示项目列表,其中包含“正在加载..”消息,我尝试在getViewAt() 记录值,如上所示,它成功显示了我想要的内容。我的代码中是否缺少导致问题的任何内容?提前感谢您的帮助

【问题讨论】:

  • 嗨,我也遇到了同样的问题。如果你有解决方案,你能帮我吗?你能把这个和ho的运行代码发给我,让kist项目可以点击吗?如果您有解决方案,请帮助我

标签: android listview listviewitem android-appwidget homescreen


【解决方案1】:

我发现了这个问题。事实证明,我需要在 WidgetRemoteViewsFactory 类中返回 getViewTypeCount() 中的视图数,在这种情况下为 1。

【讨论】:

  • 我遇到了同样的问题,我们的代码几乎相同,但我从 Web 服务获取数据。我也将getViewTypeCount 设置为返回 1,但我的ListView 仍然没有显示项目。你能帮助我吗? stackoverflow.com/questions/39266206/… 谢谢。
猜你喜欢
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多