我正在制作一个使用 listview 和 listadapter 的应用程序。它工作得很好,但我在 getgroupview 方法中发现了内存泄漏。在我每次都膨胀一个 xml 文件并导致泄漏之前。
groupRow = inflater.inflate(R.layout.activity_phrase, parent, false);
换句话说 - 我没有使用转换视图。所以我将语法更改为:
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;
}
非常感谢帮助!!!!