Reputation: 279
i would like to show data as list in my app widget. But i am new with app widget so can you help me with any example or any references? Here is my code:
public class AppWidget extends AppWidgetProvider {
protected static final String file_name ="user";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
AppWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
//Get user name
SharedPreferences settings = context.getSharedPreferences(file_name, 0);
String name = settings.getString("name", null);
//Get data from database
Database entry = new Database(context);
entry.open();
String[] myval=entry.planlist2(name);
entry.close();
// Set the text
//remoteViews.setTextViewText(R.id.app_name, String.valueOf(number));
remoteViews.setTextViewText(R.id.movie_name, Arrays.toString(myval).replaceAll("\\[|\\]", ""));
// Register an onClickListener
Intent intent = new Intent(context, AppWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.app_name, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
Now i want to show myval data as list.
Upvotes: 0
Views: 1280
Reputation: 1007359
You need to:
Add a ListView
to your RemoteViews
layout
Create a RemoteViewsService
and RemoteViewsFactory
, basically serving in the role of what an ArrayAdapter
might serve in a ListView
in an activity
Call setRemoteAdapter()
on your RemoteViews
to teach it where the service is to be able to populate the ListView
rows
This is covered in the documentation, and here is a sample app of mine demonstrating it.
Upvotes: 3