[英]Is there a way to update listView item layout in android studio?
I'm making a button that when i click on him the visibility of a checkbox withing a listView will change. 我正在制作一个按钮,当我点击他时,带有listView的复选框的可见性将会改变。 however it appears that the code run as expected but the visibailty is not udpateing.
然而,似乎代码按预期运行,但visibailty不是udpateing。 is there a way to update the item's visabilty?
有没有办法更新项目的可见性?
mButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0 ; i<calanders.size();i++){
View view = mListView.getAdapter().getView(i,null,mListView);
if(mButtonEdit.isSelected()){
print("button is selected");
CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
checkBox.setVisibility(View.GONE);
}else{
print("button is not selected");
CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
checkBox.setVisibility(View.VISIBLE);
}
}
mListView.getAdapter().
if(mButtonEdit.isSelected()){
mButtonEdit.setSelected(false);
}else{
mButtonEdit.setSelected(true);
}
}
});
You should never call the ListAdapter
's getView()
method. 你永远不应该调用
ListAdapter
的getView()
方法。 It is only supposed to be called by the system when scrolling though the ListView
. 它只应该在通过
ListView
滚动时由系统调用。 Instead you need to update the list by calling mListView.getAdapter().notifyDataSetChanged()
. 相反,您需要通过调用
mListView.getAdapter().notifyDataSetChanged()
来更新列表。
Add a boolean field in the adapter and update its value when the button is clicked. 在适配器中添加一个布尔字段,并在单击该按钮时更新其值。
You can create a model/data class based on your data and keep a Boolean variable for checkbox visibility. 您可以根据数据创建模型/数据类,并为复选框可见性保留布尔变量。 So default make it false and on button click get the position of list-view item and update Boolean variable to true, and do adapter.notifyDataSetChanged() .
因此默认使其为false,并在按钮单击时获取list-view项的位置并将Boolean变量更新为true,并执行adapter.notifyDataSetChanged() 。
You can also try with : 您也可以尝试:
((YourAdapter) yourListView.getAdapter()).notifyDataSetChanged(); ((YourAdapter)yourListView.getAdapter())。notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.