2017-01-07 2 views
0

私はアンドロイドアプリでウィジェットを作成しました。私はデータベースに接続し、いくつかの列の値を取得します。このウィジェットを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

[OK]を、私は私の問題を解決しました。

私は新しい設定を作成し、次にSharedPreference Iを使用してカウント数を保存しました。

毎回ウィジェットが更新され、SharedPreferenceから自分の番号が取得され、cursor.move(mynumber)を使用した後、mynumberの値+1を設定してもう一度保存します。

関連する問題