2012-12-28 178 views
0

我有一个类的行,它扩展RelativeLayout并膨胀一些布局自我。 A CheckBox检查状态监听器也被设置。ListView与自定义ArrayAdapter

public class LayersListRow extends RelativeLayout {   

    public LayersListRow (Context context, final GITuple tuple) { 
    super(context); 
    LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true); 
     ... 

    CheckBox checkbox = (CheckBox)findViewById(R.id.layers_list_item_switch); 

    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) { 
     ... 
     } 
    }); 
    } 
    ... 
} 

我有一个自定义ArrayAdapter,这是充满了这个类的一些对象。我也想用它们作为视图。

public class LayersAdapter extends ArrayAdapter<LayersListRow> { 

    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
     return getItem(position); 
    } 

    public LayersAdapter (Context context, int resource, int textViewResourceId) { 
     super(context, resource, textViewResourceId); 
    } 
} 

但当我这个适配器设置为我ListViewCheckBox的监听器不起作用。但对于出现在向下滚动的项目,它工作正常。

如果我

if (null == convertView) 
    return getItem(position); 

return convertedView; 

然后在列表中的第听众的伟大工程中所有可见的物品代替return getItem(position);getView方法,但是当我尝试滚动我(又是第一个)拿错物品,ListView冻结,不回应。

为什么会发生这种情况,应该如何使其正常工作?

+0

您是否介意发布适配器的整个代码? – Luksprog

+0

@Luksprog它是我的'LayersAdapter'类的整个代码:) –

+0

我对'getItem(position)'方法感兴趣。 – Luksprog

回答

0

当您使用此

if (null == convertView) 
    return getItem(position); 

return convertedView; 

被重用的视图。因此,当您滚动第一次显示的视图时,将再次显示。

我会建议你看看我在这篇文章中写的答案。如果你不能按照它在这里发表评论。

Get checked Listitems from ListView and pass that to another activity

相关问题