Question

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?

Était-ce utile?

La solution

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

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top