1

in my project,there is four buttons on the top, and when I click one,I just show a popubwindow, but how can I update popubwindow's contentview. like listview's notifidatasetchanged. I just want update popubwindow's contentview without its dismissing and recreate. and in my popubwindow is a listview,and when I change something, I invoke the listview's adapter's notifyDataSetChanged()API, but I just got a java.lang.UnsupportedOperationException. any helps thanks!

lightman1988
  • 141
  • 1
  • 13

1 Answers1

0

just update the adapter for the list view in the popupwindow.

private List<String> data = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private Handler handler = new Handler();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data.add("1");
    data.add("2");
    data.add("3");

    ListView listView = new ListView(this);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    listView.setAdapter(adapter);

    final PopupWindow popupWindow = new PopupWindow(this);
    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(listView);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.showAsDropDown(button);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    data.add(String.valueOf(data.size() + 1));
                    adapter.notifyDataSetChanged();
                }
            }, 1000);
        }
    });
}
Leo Lin
  • 681
  • 5
  • 8
  • I just do this, but Ijust got a java.lang.UnsupportedOperationException – lightman1988 May 07 '15 at 03:09
  • and in your code, every time you click the button,it invoke popubWindow.showAsDropDown(button); and if this works,the popubwindow will dissmiss and recreate again. but thanks – lightman1988 May 07 '15 at 03:15
  • After clicked the button for 1sec, the popupwindow will update without dismiss and recreate. – Leo Lin May 07 '15 at 03:35
  • You faced java.lang.UnsupportedOperationException. I guess that you use Arrays.asList as data for listview? – Leo Lin May 07 '15 at 03:35
  • Arrays.asList returning a fixed-size list so you cannot add element or you get java.lang.UnsupportedOperationException – Leo Lin May 07 '15 at 03:48