convertview в getgroupview не работает должным образом
Я делаю приложение, которое использует listview и listadapters. Это работало нормально, но я обнаружил утечку памяти в методе getgroupview. Раньше я каждый раз надувал xml-файл, и это вызывало утечку.
groupRow = inflater.inflate(R.layout.activity_phrase, parent, false);
Другими словами - я не использовал convertview. Поэтому я изменил синтаксис:
if (convertView == null) {
groupRow = inflater.inflate(R.layout.activity_phrase, parent, false);
}
else {
groupRow = convertView;
}
После этого изменения у меня возникли проблемы с флажками. если я нажимаю на флажок и прокрутки и вверх, все больше и больше флажков отмечены. Здесь я показываю изображение списка, когда я только проверял ОДИН пункт и прокручивал вверх и вниз некоторое время
А вот и весь код переопределенного метода getChildView
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//LayoutInflater inflater = (LayoutInflater) weakContext.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//WeakReference <LayoutInflater> inflaterW = new WeakReference <LayoutInflater> (inflater);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) weakContext.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
else {
groupRow = convertView;
}
final View theRow = groupRow;
final int group_Position = groupPosition;
TextView textView;
if (isExpanded) {
textView = (TextView) theRow.findViewById(R.id.groupTitle); // fetstil för grupptitel om expanderad
textView.setTypeface(null, Typeface.BOLD);
textView.setText(titles[groupPosition]);
}
else {
textView = (TextView) theRow.findViewById(R.id.groupTitle);
textView.setText(titles[groupPosition]);
textView.setTypeface(null, Typeface.ITALIC);
}
System.out.println("grupppos: " + groupPosition);
System.out.println("size checked list: " + checked.size());
CheckBox cb = (CheckBox) theRow.findViewById(R.id.check);
for (int i = 0; i < checked.size(); i++) { // kontrollera om aktuell grupp som visas i vyn är tillagd i favoriter.
if (checked.get(i) == groupPosition) {
cb.setChecked(true);
System.out.println("TRUE");
}
else {
}
}
// Lyssnare till checkboxobjektet.
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) { // Tillagd i favoriter
checked.add(group_Position); // registrera att denna position är tillagd som favorit
System.out.println("I SETONCHECKEDCHANGELISTENER");
// Lägger in nödvändiga parametrar i mysqlLite.
int n_childs = contents[group_Position].length;
switch(n_childs) { // om båda könen
case 1: sql.addPhrase(group_Position, category, titles[group_Position], contents[group_Position][0],
sound[group_Position][0]);
break;
case 2: sql.addPhrases(group_Position, category, titles[group_Position], contents[group_Position][0], // om kap o ka
contents[group_Position][1], sound[group_Position][0], sound[group_Position][1]);
break;
}
}
else { // avregistrerad från favoriter
for (int i = 0; i < checked.size(); i++) {
if (checked.get(i) == group_Position) {
checked.remove(i);
}
}
int n_phrases = sql.getRowCount();
Object[][] phrase = sql.getPhraseList();
int id = -1;
for (int i = 0; i < n_phrases; i++) {
if ((Integer) phrase[i][1] == group_Position && (Integer) phrase[i][2] == category) {
id = (Integer) phrase[i][0];
}
}
sql.deletePhrase(id); // radera fras från mysqlLite m.a.p. primärnyckel-id.
}
}
});
return theRow;
}
Спасибо за помощь!!!!!
1 ответ
Ты звонишь cb.setChecked(true)
Вы также должны позвонить cb.setChecked(false)
в соответствующей ветке else. Поскольку представления перерабатываются, состояние представления перерабатывается, и вам придется сбросить его самостоятельно.
Кроме того, кажется, что нет никакого кода, где вы вначале надуете представление (если convertView
является нулевым) но это, вероятно, просто проблема с этим вопросом.