我正在制作一个使用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;
}
很乐于帮助!!!!!
答案 0 :(得分:1)
您致电cb.setChecked(true)
,您还应在相应的其他分支中拨打cb.setChecked(false)
。由于视图被回收,视图状态也被回收,您必须自己重置。
此外,似乎没有任何代码首先让您对视图进行充气(如果convertView
为空),但这可能只是这个问题的问题。