2017-01-07 83 views
0

我在我的android应用程序中创建了一个小部件。我连接到数据库并获得一些列值。我希望这个小部件每10秒更新一次,并从我的数据库中获取下一个值。 我想用cursor.movetonext()来做,但我不确定如何在这里使用它。如何在android中使用cursor.movetonext()更新主屏幕小部件?

这是我的小部件的java类:

public class DataAppWidget extends AppWidgetProvider { 

    private String dataP; 
    private String dataE; 
    private dataRepo dataRepo; 
    private Cursor cursor; 


    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     for (int appWidgetId : appWidgetIds) { 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.data_app_widget); 
      dataRepo = new dataRepo(context.getApplicationContext()); 

       cursor = dataRepo.getallData(); 
       dataP = cursor.getString(cursor.getColumnIndex(data_P.KEY_P_NAME)); 
       dataE = cursor.getString(cursor.getColumnIndex(data_E.KEY_E_NAME)); 
       views.setTextViewText(R.id.appwidget_p, dataP); 
       views.setTextViewText(R.id.appwidget_e, dataE); 
       appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 
    } 

    @Override 
    public void onEnabled(Context context) { 
     // Enter relevant functionality for when the first widget is created 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // Enter relevant functionality for when the last widget is disabled 
    } 

} 

这是我的小部件信息的xml:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/data_app_widget" 
    android:initialLayout="@layout/data_app_widget" 
    android:minHeight="40dp" 
    android:minWidth="180dp" 
    android:previewImage="@drawable/data_appwidget_preview" 
    android:resizeMode="horizontal|vertical" 
    android:updatePeriodMillis="10000" 
    android:widgetCategory="home_screen"></appwidget-provider> 

回答

0

好吧,我解决我的问题。

我创建了新的首选项,然后使用SharedPreference我存储了计数编号。

然后,每次小部件更新我从SharedPreference获得我的号码,并使用cursor.move(mynumber),之后我将mynu​​mber值设置为+1并再次存储它。