在我的项目中,顶部有四个按钮,当我点击一个按钮时,我只显示一个popubwindow,但是如何更新popubwindow的contentview。像listview的notifidatasetchanged。我只是想要更新popubwindow的contentview而不解散并重新创建。在我的popubwindow中是一个listview,当我改变一些东西时,我调用了listview的适配器的notifyDataSetChanged()API,但我得到了一个java.lang.UnsupportedOperationException。任何帮助谢谢!
答案 0 :(得分:0)
只需在弹出窗口中更新列表视图的适配器。
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);
}
});
}