Android 列表视图中的数据重复
我有一个列表视图,其适配器位于不同包中的不同类中。现在我必须从数据库获取数据,然后使用该数据为列表视图设置适配器。因此,我创建了一个 ArryList 并将其传递到 Adapter 的构造函数中,同时在列表视图中查看它。但问题是数据是重复的。例如,数组列表中有 12 个不同的字符串,但我得到的是 - 按顺序排列的前五个元素,之后重复相同的五个元素。数据的计数始终是正确的,但位置始终是 0,1,2,3,4 。我不明白问题是什么。这是代码-
public class CheckboxAdapter extends BaseAdapter{
LayoutInflater inflater ;
ArrayList<String> mData = new ArrayList<String>();
//constructor for lesion adapter
public CheckboxAdapter (Context context, ArrayList<String> data){
inflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
}//end of if condition
return convertView;
}
I have a list view whose adapter is in a different class in a different package. Now I have to get the data from database then I set Adapter for my list view using this data. So I have created an ArryList and pass this in the constructor of Adapter while seeting it for the list view. But the problem is that the data is repeating. eg.- there are 12 distinct Strings in the arraylist but what I get is -first five elements in order and after that the same five are being repeated. The count of data is always right but the position will always be 0,1,2,3,4 . I can not understand what the problem is . here is the code -
public class CheckboxAdapter extends BaseAdapter{
LayoutInflater inflater ;
ArrayList<String> mData = new ArrayList<String>();
//constructor for lesion adapter
public CheckboxAdapter (Context context, ArrayList<String> data){
inflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
}//end of if condition
return convertView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个..只需将所有代码写出期望膨胀代码的条件..
try this.. just write all code out of the condition expect inflate code..