[英]Why is a boolean value in JTable showing as true/false instead of a checkbox?
[英]Checkbox value is true but tick is not showing - Android / Java
基本上,我在三(3)個不同的片段中有三(3)個復選框列表。 當我打開活動時,默認片段中的復選框顯示為正常,但不同片段中的復選框顯示為不同。 復選框的值仍然為true。 下面是復選框的圖像,它使內容和我的代碼更加清晰。
第一個片段頁面:
第二個片段頁面:
設置復選框及其if condition
private void setCheckBox(ArrayList<DataPrefType> arrayList){
for(int i = 0; i < arrayList.size(); i++){
id = i + 1;
checkBox = new CheckBox(getActivity());
checkBox.setId(i + 1);
checkBox.setText(arrayList.get(i).getName());
checkBox.setPadding(10, 10, 10, 10);
checkBox.setLayoutParams(params);
checkBox.setGravity(Gravity.CENTER);
checkBoxLayout.addView(checkBox);
if(!userPreference.isEmpty() || userPreference != null){
for(int j = 0; j < userPreference.get(0).getDataPrefTypeArrayList().size(); j++){
int retrieveId = Integer.parseInt(userPreference.get(0).getDataPrefTypeArrayList().get(j).getId());
if(checkBox.getId() == retrieveId)
checkBox.setChecked(true);
}
}
即使isChecked為true,帶有復選框也未顯示為已選中的bug處於繪制動畫中。
我在這里找到了這個解決方案。 我將其發布在此處作為答案,以使其更易於查找,並包括有關如何調用它的一些其他信息。
嘗試在根視圖(或復選框的父視圖)上調用jumpDrawablesToCurrentState
。 如果專門調用checkBox.setChecked(true);
與OP一樣,請在此后調用。 在這個問題中,在將所有復選框都添加到父視圖之后調用此函數很重要。 本質上,這將在當前狀態下繪制所有可繪制對象,從而跳過動畫過程。
例:
@Override
public void onStart() {
View v = getView();
if(v != null) {
CheckBox chkYourCheckBox = (CheckBox)v.findViewById(R.id.chkYourCheckBox);
chkYourCheckBox.setChecked(true); //sometimes this may draw fine, and other times it may not
//***The important line:
v.jumpDrawablesToCurrentState();
}
}
您可以覆蓋Fragment的setUserVisibleHint()
方法並在其中的復選框中加載。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser)setCheckBox();
}
這只是一個解決方法,我認為這不是解決問題的最佳方法,但是我還沒有發現更好的方法。 這可能導致Null指針異常。
我創建了此Kotlin擴展屬性來解決該問題(顯然,僅在編寫Kotlin代碼而不是Java時有用)
/**
* Set the state of a checkbox skipping animations.
*/
var CheckBox.checked: Boolean
get() = isChecked
set(value) {
if(isChecked != value) {
isChecked = value
jumpDrawablesToCurrentState()
}
}
現在在我的代碼中,我寫的是checkbox_view.checked = true
而不是checkbox_view.isChecked = true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.