Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3613762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:09:53+00:00 2026-05-18T22:09:53+00:00

I am writing an app widget which I intend to populate with a list

  • 0

I am writing an app widget which I intend to populate with a list of items. I’m trying to do it the easy way by extending AppWidgetProvider. I am seeing some strange behaviour where the list of items gets added to the parent widget twice.

Here’s the code:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.i("MyApp", "onUpdate called");

    DbAdapter dbAdapter = new DbAdapter(context);
    dbAdapter.open();

    final String[] columns = { DbTableCategory.KEY_NAME, DbTableCategory.KEY_CURRENTBAL };
    Cursor cursor = dbAdapter.getDb().query(DbTableCategory.TABLE_NAME, columns, null,
                        null, null, null, null);

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

        Log.i("MyApp", "Widget instance " + i);

        final int NUM_ITEMS = 4;

        int id = appWidgetIds[i];
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        for (int c = 0; c < NUM_ITEMS; c++) {

            // get item info from db
            if (cursor.moveToPosition(c)) {
                RemoteViews itemView = new RemoteViews(context.getPackageName(), R.layout.widget_item);
                itemView.setTextViewText(R.id.widget_item_name, cursor.getString(0));
                itemView.setTextViewText(R.id.widget_item_amnt, cursor.getString(1));

                Log.i("MyApp", "Adding subview for item " + c);
                rv.addView(R.id.widget_container, itemView);
            }
        }

        appWidgetManager.updateAppWidget(id, rv);
    }

    cursor.close();
    dbAdapter.close();

I add four items to the list, but I actually see eight items in the widget (the same four appearing twice). From the log output, it is telling me that there are two instances of the widget (according to the appWidgetIds array, and the IDs are different), so the outer loop is running twice, and the inner loop (for each item) is running four times as expected. I don’t understand this, as I’m positive I’ve only added the widget to my home screen once. It’s not on any of the other home screens either – I’m using the default HTC Sense launcher.

Even if I had instantiated the widget twice, I’m creating a new RemoteViews for each widget instance. I just don’t understand why the one widget instance seems to be receiving two lots of items. What am I getting wrong?

Addendum: when I run the exact same code in the emulator, it works just fine, with just one widget instance being reported. It’s only showing the strange behaviour on the actual phone (HTC Desire, Froyo).

Edit: I’ve since noticed this in the documentation for RemoteViews.addView():

In cases where consumers of RemoteViews may recycle layouts, use removeAllViews(int) to clear any existing children.

Could it be that the same RemoteViews instance is being returned from the RemoteViews(...) constructor? That would explain why the child views are being added twice, but it would not explain why there seem to be two instances in the first place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:09:53+00:00Added an answer on May 18, 2026 at 10:09 pm

    The first problem about your widget returning two appWidgetIds:
    I had similar situations when the widget crash (especially on configuration screens or before the widget was fully placed). The desktop app will still contain information about it, but it won’t display it.

    The second (main) problem is probably caused by recycling of layout views, my guess is that R.id.widget_container is not cleaned up. So you probably should call:

    rv.removeAllViews(R.id.widget_container);
    

    before the for loop.

    In addition I would suggest getting the number of returned rows from the cursor (cursor.getCount()) and not hard-coding it (NUM_ITEMS = 4 in your code).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an Android widget. I have MyAppWidgetProvider which extends AppWidgetProvider. During the widget's
I'm writing an App and an accopmanying widget that needs data. This data is
I'm writing an app for gnome which will support plugins. Each plugin will contain
I am writing an app. There are plugins which DL resource from the net.
I'm trying to make a real simple app in which clicking a button has
So I'm writing an app, and for some reason I get a Runtime exception
This is my first attempt at writing a QT app, and I'm just trying
Im writing an app with voice activated buttons. I would like the buttons on
I'm currently writing an app in Python and need to provide localization for it.
I'm currently writing an app in Android that works with the GPS. At the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.