Android getgroupview中的convertview工作不正常
我正在制作一个使用listview和listadapters的应用程序。它工作得很好,但我在getgroupview方法中发现了一个memoryleak。在我每次膨胀xml文件之前,这导致了泄漏Android getgroupview中的convertview工作不正常,android,android-listview,expandablelistview,expandablelistadapter,convertview,Android,Android Listview,Expandablelistview,Expandablelistadapter,Convertview,我正在制作一个使用listview和listadapters的应用程序。它工作得很好,但我在getgroupview方法中发现了一个memoryleak。在我每次膨胀xml文件之前,这导致了泄漏 groupRow = inflater.inflate(R.layout.activity_phrase, parent, false); 换句话说,我没有使用convertview。因此,我将语法更改为: if (convertView == null) { groupRow
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;
}
在这次更改之后,我遇到了checkboxitems的问题。如果我点击一个复选框并上下滚动,就会选中越来越多的复选框。在这里,当我只检查了一个项目并上下滚动了一段时间后,我显示了一个列表的图像
下面是被重写的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;
}
public View getGroupView(int-groupPosition、boolean-isExpanded、View-convertView、View-group-parent){
//LayoutFlater充气器=(LayoutFlater)weakContext.get().getSystemService(Context.LAYOUT\u充气器\u服务);
//WeakReference充气机W=新的WeakReference(充气机);
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)weakContext.get().getSystemService(Context.LAYOUT\u充气器\u服务);
}
否则{
groupRow=convertView;
}
最终视图theRow=组行;
最终整数组位置=组位置;
文本视图文本视图;
如果(扩展){
textView=(textView)theRow.findviewbyd(R.id.groupTitle);//fetstil föR gruptitel om expanderad
textView.setTypeface(null,Typeface.BOLD);
setText(标题[groupPosition]);
}
否则{
textView=(textView)theRow.findViewById(R.id.groupTitle);
setText(标题[groupPosition]);
textView.setTypeface(null,Typeface.ITALIC);
}
System.out.println(“gruppppos:+groupPosition”);
System.out.println(“大小检查列表:+checked.size());
复选框cb=(复选框)theRow.findViewById(R.id.check);
对于(int i=0;i
非常感谢您的帮助 如果调用
cb.setChecked(true)
,还应该在相应的else分支中调用cb.setChecked(false)
。由于视图被回收,视图状态也被回收,您必须自己重新设置
另外,在您首先膨胀视图的地方似乎没有任何代码(如果
convertView
为null),但这可能只是这个问题的一个问题。如果调用cb.setChecked(true)
,您还应该在相应的else分支中调用cb.setChecked(false)
。由于视图被回收,视图状态也被回收,您必须自己重新设置
此外,似乎没有任何代码首先膨胀视图(如果
convertView
为null),但这可能只是这个问题的一个问题。我应该提到,我之前在else bransch中放置了cb.setChecked(false),但没有成功。发生的情况正好相反——如果选中的项目被滚动出视图,然后又返回,那么它将被取消选中。我查看了Vogella的ListView示例。也许我应该实现它——例如,将侦听器放在if bransch中,当convertview被激活时,它被激活null@user2365568围绕它的for循环使事情变得更加复杂。默认情况下,复选框应该被取消选中,如果for循环找到匹配项,则将其设置为checked。现在,存储数据的sqlitedatabase也被扭曲了。奇怪的是,当滚动时,监听器(setOnCheckedChangeListener)会对此作出反应,而不仅仅是在选中复选框时。我就是搞不懂——为什么听众时不时地听滚动。当它侦听时,意味着数据被添加到mysqlite。也许14小时后我应该休息一下。:-)我应该