Мое приложение
использует ViewPager
и Fragements
.Каждый Fragment
показывает ListView
с CheckBoxes
, ProgressBar
и TextView
.Если проверяются 3 из 6 CheckBoxes
, ProgressBar
и TextView
должны обновиться до 3%.
Проблема
TextView и ProgressBar не отображают обновление должным образом.Я записал рассчитанное значение прогресса и значение tv.getText()
и pb.getProgress()
.Есть две проблемы:
1.) Значение прогресса и значения tv.getText()
и pb.getProgress()
не равны.Значение, возвращаемое tv.getText()
и pb.getProgress()
, всегда на 3% слишком высокое или слишком низкое.
(И да, значения, которые я передаю в setProgress () и setText (), верны)
11-30 14:44:57.099 14230-14230/com.example..myapplication
D/CheckBox: setProgressBar()
progress should be = 3%
progressBar is = 0%
progressText is = 0%
11-30 14:45:02.275 14230-14230/com.example..myapplication
D/CheckBox: setProgressBar()
progress should be = 7%
progressBar is = 3%
progressText is = 3%
2.) В представлении TextView
и ProgressBar
всегда отображается 0%.
Я пытался исправить это часами, и я даже не могу найти решение здесь, на StackOverflow.Что не так?
Класс фрагмента
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
progressBar = view.findViewById(R.id.progressBar);
progressText = view.findViewById(R.id.progressText);
lv = view.findViewById(R.id.list_view_nahrungsmittel);
dateTV = view.findViewById(R.id.changeDate);
leftBTN = view.findViewById(R.id.changeDateBTNLeft);
rightBTN = view.findViewById(R.id.changeDateBTNRight);
backToTodayBTN = view.findViewById(R.id.backToTodayBTN);
mDB = new DBHelper(getContext()); //hier oder main?
items = MainActivity.getList();
setBTN();
setBackToTodayButton();
setNumCheckedCB();
setDateTV();
setProgressBarFirstTime();
setListAdapter();
setListViewListener();
setButtons();
return view;
}
public void setProgressBar(int i) {
Log.d("CheckBox", "setProgressBar()");
progress = (int)(((float)i * 100f) / (float) MainActivity.getNumCB());
Log.d("CheckBox", "progress should be = " + progress + "%" );
Log.d("CheckBox", "progressBar is = " + progressBar.getProgress() + "%" );
Log.d("CheckBox", "progressText is = " + progressText.getText() );
progressBar.setProgress(progress);
progressText.setText(progress + "%");
}
ListAdapter
устанавливает прослушиватель для каждого CheckBox в ListView, который вызывает метод setProgressBar ().
//Click on CB
final DataHolder finalHolder1 = holder;
holder.cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isPressed()) {
buttonView.setChecked(isChecked);
//Update Progress
int i = 0;
boolean[] stateCheckBox = mDB.getCheckBoxState(date, item.getTitle());
if(isChecked){
if(finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE && finalHolder1.cb3.getVisibility() == View.VISIBLE ){
if(stateCheckBox[1] == false && stateCheckBox[2] ==false) { i = 3; }
else if (stateCheckBox[1] == false && stateCheckBox[2] ==true) { i = 2; }
else if (stateCheckBox[1] == true && stateCheckBox[2] ==true) { i = 1; }
}
else if (finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE) {
if (stateCheckBox[1] == false) { i = 2; }
else if (stateCheckBox[1] == true) { i = 1; }
}
else { i = 1; }
}
else { i = 1; }
updateNumCheckedCheckBoxes(date, isChecked, i);
//Update CB state
//Case1: Three CB in row, Left gets checked -> set all other right CB´s checked
if (isChecked) {
finalHolder1.cb2.setChecked(isChecked);
finalHolder1.cb3.setChecked(isChecked);
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, isChecked, isChecked);
} else {
if (finalHolder1.cb2.getVisibility() == View.GONE) {
//Case2: Only one Cb in row, uncheck
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, isChecked, isChecked);
} else if (finalHolder1.cb3.getVisibility() == View.GONE) {
//Case3: Two Cb in row, left gets unchecked
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, !isChecked, !isChecked);
} else {
//Case4: Three CB, left gets unchecked
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, !isChecked, !isChecked);
}
}
}
}
});
holder.cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isPressed()) {
buttonView.setChecked(isChecked);
//Update Progress
int i = 0;
boolean[] stateCheckBox = mDB.getCheckBoxState(date, item.getTitle());
if(isChecked){
if(finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE && finalHolder1.cb3.getVisibility() == View.VISIBLE ){
if(stateCheckBox[2] ==false) { i = 2;}
else if (stateCheckBox[2] ==true) { i = 1;}
}
else if (finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE) { i = 1; }
}
else {
if(finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE && finalHolder1.cb3.getVisibility() == View.VISIBLE ){
if(stateCheckBox[0] ==false) { i = 1; }
else if (stateCheckBox[2] ==true) { i = 2; }
}
else if (finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE) {
if(stateCheckBox[0] == true) { i = 2; }
else if (stateCheckBox[0] == false) { i = 1;}
}
}
updateNumCheckedCheckBoxes(date, isChecked, i);
//Update CB state
//Case5: Second from the left CB gets checked -> set right CB checked
if (isChecked) {
finalHolder1.cb3.setChecked(isChecked);
mDB.updateCheckBoxState(date, item.getTitle(), !isChecked, isChecked, isChecked);
}
//Case6: Second from the left CB gets unchecked -> set left CB unchecked
else {
finalHolder1.cb1.setChecked(isChecked);
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, isChecked, !isChecked);
}
}
}
});
holder.cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isPressed()) {
buttonView.setChecked(isChecked);
//Update Progress
int i = 0;
boolean[] stateCheckBox = mDB.getCheckBoxState(date, item.getTitle());
if(isChecked){ i = 1; }
else {
if(finalHolder1.cb1.getVisibility() == View.VISIBLE && finalHolder1.cb2.getVisibility() == View.VISIBLE && finalHolder1.cb3.getVisibility() == View.VISIBLE ){
if(stateCheckBox[0] == false && stateCheckBox[1] == false) { i = 1; }
else if (stateCheckBox[0] == false && stateCheckBox[1] ==true) { i = 2; }
else if (stateCheckBox[0] == true && stateCheckBox[1] == true) { i = 3; }
}
}
updateNumCheckedCheckBoxes(date, isChecked, i);
//Update CB state
//Case5: Right CB gets unchecked -> set all other left CB´s unchecked
if (!isChecked) {
finalHolder1.cb2.setChecked(isChecked);
finalHolder1.cb1.setChecked(isChecked);
mDB.updateCheckBoxState(date, item.getTitle(), isChecked, isChecked, isChecked);
}
else {
//Case 6: 3CBs, only right CB gets checked
mDB.updateCheckBoxState(date, item.getTitle(), !isChecked, !isChecked, isChecked);
}
}
}
});
//Click on Image Button
holder.imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent intent = new Intent(context , Statistic2Activity.class);
context.startActivity(intent);
}
});
}
private void updateNumCheckedCheckBoxes (String date, boolean isChecked, int i){
mDB.updateNumCheckedCheckBoxes(date, isChecked, i);
updateProgressBar(date);
}