4

I have a listView. I needed a custom view for each list item, so I've created a custom ListAdapter which gives the views, layout for which is given below. But how do I set this listAdapter to the ListView in the widget using RemoteViews ?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/background_light"
    android:layout_margin="1sp"
    android:padding="10sp"
    >

    <TextView
        android:id="@+id/widgetInfo1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/listItemDummy"
        android:textColor="@android:color/black"
        />

    <TextView
        android:id="@+id/widgetInfo2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/listItemDummy"
        android:textColor="@android:color/black"
        />

</LinearLayout>
lokoko
  • 5,785
  • 5
  • 35
  • 68
Amol
  • 285
  • 1
  • 5
  • 15
  • Do you want a listadapter for this listview or do you want it for a remoteview ? also what have you tried ? – lokoko Feb 09 '13 at 06:06
  • I want to add ListView to a widget, which has the custom view items. That is my basic requirement, so I created a listAdapter but it seems that you can not use the normal listAdapter and add to ListView in widget.. Above layout is for the single view item in list... – Amol Feb 09 '13 at 07:09

1 Answers1

5

Instead of the common Adapter, for RemoteViews you need to implement RemoteViewsFactory. To return custom RemoteView's (yes, RemoteView for each item and not View), you will need to override its getViewAt(int position) method.

Also, one just does not setAdapter() for RemoteViews, you would need to provide a RemoteViewsService which will return above RemoteViewsFactory to its clients.

Finally, to the client that is going to show the RemoteViews, you pass an Intent of this service.

The Service and its Intent you will need to declare in your manifest file. This will make your App a provider of remote List like views to other apps or any remote process.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Are you suggesting something like: remoteViewsServiceIntent = new Intent( context , MyRemoteViewsService.class); – Amol Feb 09 '13 at 13:32
  • @AmolKamble http://developer.android.com/guide/topics/appwidgets/index.html#collections – S.D. Feb 09 '13 at 15:07
  • How to pass custom data (my list of items from API) to this adaptor? – Thiago Jan 25 '22 at 14:45