문제

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?

도움이 되었습니까?

해결책

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

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top