Estoy tratando de crear un widget con información cargada desde un Sqlite. El problema que tengo es que el widget en si no carga dicho contenido. Adjunto código.
widget_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/widget_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary" />
widget_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="72dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<TextView
android:id="@+id/stock_symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
tools:text="List Item"
/>
</LinearLayout>
Widget Provider
public class ToDoTickWidget extends AppWidgetProvider {
public static final String UPDATE_MEETING_ACTION = "android.appwidget.action.APPWIDGET_UPDATE";
public static final String EXTRA_ITEM = "com.example.edockh.EXTRA_ITEM";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//views.setOnClickPendingIntent(R.id.stock_symbol, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@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) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@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
}
}
Servicio
public class ListViewWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new ListViewRemoteViewsFactory(this.getApplicationContext(), intent);
}
y por ultimo el factory
public class ListViewRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private Cursor cursor;
private Intent intent;
public ListViewRemoteViewsFactory(Context context, Intent intent){
this.context = context;
this.intent = intent;
}
private void initCursor(){
if (cursor!=null){
cursor.close();
}
final long identityToken = Binder.clearCallingIdentity();
cursor = new ListFacade(context).RowsCursor();
Binder.restoreCallingIdentity(identityToken); }
@Override
public void onCreate() {
initCursor();
if (cursor!=null){
cursor.moveToFirst();
}
}
@Override
public void onDataSetChanged() {
initCursor();
}
@Override
public void onDestroy() {
cursor.close();
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_item);
cursor.moveToPosition(position);
System.out.println("--->"+cursor.getCount()+"<---");
remoteViews.setTextViewText(R.id.stock_symbol, cursor.getString(1));
return remoteViews;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
}
Como obtengo el cursor
public Cursor RowsCursor(){
SQLiteDatabase conn = new SqliteHandler(context).getReadableDatabase();
Cursor cursor = conn.rawQuery("SELECT ROWID,* from TODO_LIST", null);
return cursor;
}
Y el resultado del widget es quedar en blanco