Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling ListView in homescreen widget in Android

I'm trying to create an android widget containing a list of elements (tasks).

When i place the widget on the homescreen it's empty, no one task is displayed.

I don't unnderstand what is missing.

Thanks in advance for your help.

Here is the xml for my widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/title"
    style="@android:style/TextAppearance.Medium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="4dip"
    android:gravity="center_horizontal"
    android:text="@+string/TASK_LIST_ACT" >
</TextView>

<ListView
    android:id="@+id/list"
    style="@android:style/TextAppearance.Medium"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

This is the layout for the items of the ListView

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight" />

This is my widgetProvider class

package it.geotask.android.widget;

import it.geotask.android.R;
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 TaskWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int i = 0; i < appWidgetIds.length; ++i) {

        Intent intent = new Intent(context, GeotaskRemoteViewService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews rv = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);

        rv.setRemoteAdapter(appWidgetIds[i], R.id.list, intent);

        rv.setEmptyView(R.id.list, R.id.empty_view);
        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
    }

}
}

And this is my RemoteViewsFactory

package it.geotask.android.widget;

import it.geotask.android.R;
import it.geotask.android.dto.Task;
import it.geotask.android.service.TaskService;

import java.util.ArrayList;
import java.util.List;

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

public class GeoTaskRemoteViewsFactory implements
    RemoteViewsService.RemoteViewsFactory {

private Context context = null;
private int appWidgetId;

private List<Task> taskList = new ArrayList<Task>();

public GeoTaskRemoteViewsFactory(Context context, Intent intent) {
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);


}

public int getCount() {
    return taskList.size();
}

public long getItemId(int position) {
    return taskList.get(position).getId();
}

public RemoteViews getLoadingView() {
    // TODO Auto-generated method stub
    return null;
}

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

    row.setTextViewText(android.R.id.text1, taskList.get(position)
            .getName());


    return (row);
}

public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 0;
}

public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

public void onCreate() {
    taskList = TaskService.getTaskInStateList(context, context
            .getResources().getString(R.string.DB_STATE_ACTIVE));
    taskList.addAll(TaskService.getTaskInStateList(context, context
            .getResources().getString(R.string.DB_STATE_IN_RANGE)));
    taskList.addAll(TaskService.getTaskInStateList(context, context
            .getResources().getString(R.string.DB_STATE_DELAYED)));

}

public void onDataSetChanged() {
    // TODO Auto-generated method stub

}

public void onDestroy() {
    // TODO Auto-generated method stub

}

}

The remoteViewsService simply returns an instance of the factory.

EDIT: i forgot to declare the service in the manifest; now i get the widget filled with the correct number of items, but with the message "loading" instead of the name of the item i want to display

like image 676
mrcBobLee Avatar asked Jan 02 '13 11:01

mrcBobLee


People also ask

What is a list view android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.

What is setAdapter in android studio?

setAdapter(new ArrayAdapter<String>(this,R. layout. drawer_list_item, mServices)); This line is mapping an array of strings ( mServices ) for display in a ListView ( mDrawerList ).

What is list views?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.


1 Answers

Solved. I was following the google example of stackWidget, and they use a for on the widgetIds array, incrementing the int cursor with ++i, it causes the widget not to work. Used i++ and it works

like image 126
mrcBobLee Avatar answered Dec 04 '22 12:12

mrcBobLee