1

I want to create updatable listview.
I found great example here:
https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

But ListView there is static. I want to be able to update listview that I use on Homescreen Widget from app mainActivity.

For example:
I have some app activity:
With only 2 elements: EditText and Button

And I have some homescreen widget for this app with ListView I want to be able to add item to the ListView when I clicking on Button in main app.
Is it possible?

Laser
  • 6,652
  • 8
  • 54
  • 85

1 Answers1

3

Sure, you can catch an intent in your widget sent from the Button onClick listener.

Create an intent in your main app:

public void onClick(View v) {
    Intent intent = new Intent(context, YourWidget.class);
    intent.putExtra("data", "some data");
    context.sendBroadcast(intent);
}

catch the intent in your widget:

public void onReceive(Context context, Intent intent) 
{
    ListView lv = // your list

    // get data from the intent
    String data = intent.getStringExtra("data");

    // PREPARE YOUR view object

    lv.addView(view, position); // <- add to a specific position
    lv.addFooterView(view); //<- add to the bottom of the list

}
Remarkable
  • 549
  • 1
  • 3
  • 10
  • Thanks, but how should I add `item` to the `listview`. And button is in the `mainapp` not on `Widget`. – Laser Sep 03 '13 at 08:28
  • I added some more details to the example. – Remarkable Sep 03 '13 at 08:47
  • ListView lv = // your list Are you sure that is possible in homsecreen Widget – Laser Sep 03 '13 at 09:01
  • no, I am not sure. I assumed you already placed one on your widget, but you don't know how to manipulate it. Have a look in here: http://stackoverflow.com/questions/11039339/how-can-i-build-an-appwidget-with-a-listview-while-using-the-support-library – Remarkable Sep 03 '13 at 09:13