What you are looking for is AppWidgets using Collections. This can accept a ListView
as the widget for presenting your data.
This will basically consist of
an AppWidgetProvider, as in any widget
a RemoteViewsService / RemoteViewsFactory, responsible for producing the views in your ListView, much like an Adapter would do.
Since you are not sharing your code, it's hard to say what is not working for you, but you can find working examples in the sdk samples. Note that the samples have apparently been removed from latest sdk versions, so you should probably download the samples for older sdk's, if I remember correctly. You can also browse the source here.
EDIT :
adapt=new ContactListAdapter(MainActivity.this, aa);
listcontact.setAdapter(adapt);
This will not work in app-widgets, for 2 reasons :
You don't have an Activity available. For widgets, you can use the Context provided as argument of the
onUpdate
callback in yourAppWidgetProvider
, although in your case it probably won't be needed.You don't instantiate an
Adapter
for app-widgets. To bind data to yourListView
on your homescreen, you call setRemoteAdapter on the mainRemoteView
inside yourAppWidgetProvider
(in theonUpdate
method), like this :
Example :
// Setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Creating the main RemoteView and binding data to it.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rv.setRemoteAdapter(R.id.listview, intent);
Basically, the RemoteViewsFactory's
getViewAt
method acts much like the getView
in an Adapter
would, you just have to copy and adapt the code of your adapter to make it work in the RemoteViewsFactory
. The sample of StackWidget contains a RemoteViewsFactory
where all the method are commented to let you know where to perform which tasks.
EDIT 2 :
@Override
public int getCount() {
// This method tells the factory how many rows it has to produce
// you should be returning the size of your data collection
// so in your case :
return mWidgetItems.size();
}
Also, in you getViewAt
method you are setting the text as the toString()
of the array (this is why you have all contacts in one row), whereas you should read the fields of the individual Info
object for the position given by the method:
// get the current Info object by position :
Info currentInfo = mWidgetItems.get(position);
// then use that object to fill each row, like in getView for an Adapter, for example :
rv.setTextViewText(R.id.textrow1, currentInfo.phone);
rv.setTextViewText(R.id.textrow2, currentInfo.name);