我实现了一下简答的集合widget,我想让listview在我的widget 中展示,但是 listView一直处于加载状态

文章标签: android,android-listview
2014-11-3 18:03:46     人阅读    

  我实现了一下简答的集合widget,我想让listview在我的widget 中展示,但是 listView一直处于价值状态,哪怕我关掉了数据库连接,listView还是一直加载中。

  下面是我AppWidgetProvider 类中的 onUpdate  方法

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int length = appWidgetIds.length;
    for (int x = 0; x < length; x++){
        RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        Intent intent = new Intent(context,RViewsService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[x]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remote.setRemoteAdapter(R.id.list,intent);
        remote.setEmptyView(R.id.list,R.id.empty);
        remote.setTextViewText(R.id.title,Utilities.setDayOfWeek());
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        Intent AddmateriaIntent = new Intent(context,AddMateria.class);
        PendingIntent pendingItentForButtonAddMateria = PendingIntent.getActivity(context,0,AddmateriaIntent,0);
        remote.setOnClickPendingIntent(R.id.add,pendingItentForButtonAddMateria);
        remote.setOnClickPendingIntent(R.id.empty, pendingItentForButtonAddMateria);
        appWidgetManager.updateAppWidget(appWidgetIds[x],remote);
    }
   super.onUpdate(context,appWidgetManager,appWidgetIds);
}

经过测试onUpdate 在调用的时候没有报错,下面是 RemoteViewService 和 RemoteViewsFactory 他们也没有错误

 

public class RViewsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new RViewFactory(getApplicationContext(),intent);
}

class RViewFactory implements RemoteViewsFactory {

    private DBAdapter adapter;
    private ArrayList materias;
    private Context context;

    public RViewFactory(Context context,Intent intent){
        this.context = context;
    }

    @Override
    public void onCreate() {
        adapter = new DBAdapter(context).openReadableDatabase();
        materias = new ArrayList();
        Cursor cursor = adapter.query(Utilities.setDayAndTable(), new String[]{DBAdapter.ROW_SUBJECT,DBAdapter.ROW_COLOR_NAME,DBAdapter.ROW_COLOR_HEX}, null, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    Materia materia = new Materia();
                    materia.setSubject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_SUBJECT)));
                    materia.setColor(new ColorObject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_COLOR_NAME)),
                            (cursor.getInt(cursor.getColumnIndex(DBAdapter.ROW_COLOR_HEX)))));
                    materias.add(materia);
                    Log.d("log", "Data: " + materia.getSubject());
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
    }

    @Override
    public void onDataSetChanged() {

    }

    @Override
    public void onDestroy() {
        materias = null;
        adapter.close();
    }

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

    @Override
    public RemoteViews getViewAt(int i) {
        RemoteViews remote = new RemoteViews(context.getPackageName(),android.R.layout.simple_list_item_1);
        remote.setTextViewText(android.R.id.text1,materias.get(i).getSubject());
        remote.setTextColor(android.R.id.text1,materias.get(i).getColor().getColor());
        return remote;
    }

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

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

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

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

在Widget 加载 的时候 LogCat 找不到错误或者警告信息,调用是的 LogCat 信息如下

Logcat
05-02 12:54:30.796  21885-21885/schooltimetable.app D/cannon﹕ Data: Math
05-02 12:54:30.806  21885-21885/schooltimetable.app D/cannon﹕ Data: Theory


我能确认已经取到了数据

 AppWidgetProvider  RemoteViewService: 声明如下

<receiver android:name="schooltimetable.app.appWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/cannon_widget" />
    </receiver>
    <service android:name=".RViewsService" android:permission="android.permission.BIND_REMOTEVIEWS"/>

我的测试环境 Nexus 7 (2012) : Android 4.4.2

处理方法

getViewTypeCount 返回值设置为1

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


原文地址:http://www.itmmd.com/201411/113.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

java android listview更新后位置变动   上一篇
下一篇  android 开发,在viewPager 展示listView ,listView标题bug
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!