Showing list of text App widget - Android



I try to list some text in an app widget. When I run my widget I have no error but I my widget is empty. I mean I have juste the launcher icon


Can you please help me to know why ?


This is my code :


WidgetService:



import android.content.Intent;
import android.widget.RemoteViewsService;


public class WidgetService extends RemoteViewsService
{
@Override
public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent intent)
{
return(new WidgetDisplay(this.getApplicationContext(),
intent));
}
}


Widget Provider :



import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider
{
public static String EXTRA_WORD=
"com.commonsware.android.appwidget.lorem.WORD";

@Override
public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
for (int i=0; i<appWidgetIds.length; i++)
{
Intent svcIntent=new Intent(ctxt, WidgetService.class);

svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
R.layout.widget);

widget.setRemoteAdapter(appWidgetIds[i], R.id.words,
svcIntent);

appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}

super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
}
}


WidgetDisplay :



import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;


public class WidgetDisplay implements RemoteViewsService.RemoteViewsFactory
{
private static final String[] items={"Red", "Green", "Blue", "Pink", "Orange"};
private Context ctxt=null;
private int appWidgetId;

public WidgetDisplay(Context ctxt, Intent intent)
{
this.ctxt=ctxt;
appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}

@Override
public void onCreate()
{
// no-op
}

@Override
public void onDestroy()
{
// no-op
}

@Override
public int getCount()
{
return(items.length);
}

@Override
public RemoteViews getViewAt(int position)
{
RemoteViews row=new RemoteViews(ctxt.getPackageName(),
R.layout.row);

row.setTextViewText(android.R.id.text1, items[position]);

Intent i=new Intent();
Bundle extras=new Bundle();

extras.putString(WidgetProvider.EXTRA_WORD, items[position]);
i.putExtras(extras);
row.setOnClickFillInIntent(android.R.id.text1, i);

return(row);
}

@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);
}

@Override
public void onDataSetChanged()
{
// no-op
}
}


Manifest :



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">



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

<receiver android:name="fr.orsay.iut.widgetgallery29.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

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


<service
android:name="fr.orsay.iut.widgetgallery29.WidgetService"
android:enabled="true"
android:exported="true" >
</service>

<activity android:name=".WidgetDisplay">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />

</intent-filter>
</activity>


</application>

</manifest>


WidgetProvider.xml :



<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="146dip"
android:layout_width="146dip"
android:layout_height="146dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget"
android:autoAdvanceViewId="@+id/words"
android:resizeMode="vertical"
/>


Widget.xml :



<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/words"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
/>


Row.xml :



<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>

No comments:

Post a Comment