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!
Asked
Active
Viewed 1,699 times
1
-
Everytime when you click on it, it recreates right ? Are you using ListPopUpWindow ? – Bullionist May 07 '15 at 02:03
-
yes, everytime it dismiss and recreate,I just use a normal PopupWindow. – lightman1988 May 07 '15 at 02:28
1 Answers
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