Position problem in Custom list adapter after notifydatasetchange?
friends,
i am using following custom adapter for android listview problem i am facing is wrong position after calling notifydatasetchanged();
first time when i bind data with list position 开发者_StackOverflow社区works perfect but after when i call notifydatasetchanged(); i get position parameter wrong
please guide what mistake am i doing any help would be appreciated.
public EfficientAdapter(Context context,List<DalCategories> value) {
mInflater = LayoutInflater.from(context);
obj = value;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
ViewHolder holder;
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.list_category_item, null);
holder = new ViewHolder();
holder.TextTitle = (TextView) convertView.findViewById(R.id.list_item_title);
holder.checkBoxes = (CheckBox) convertView.findViewById(R.id.chkSubCategory);
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
// while setting checkbox state after notify datasetchanged i get wrong position
if(obj.get(position).isIs_Selected())
{
holder.checkBoxes.setChecked(true);
}
holder.checkBoxes
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if(obj.get(position).isIs_Any() && isChecked)
{
if( adapter != null)
{
adapter.notifyDataSetChanged();
}
}
}
});
holder.TextTitle.setText(obj.get(position).getTitle());
return convertView;
}
class ViewHolder
{
public TextView TextTitle;
public CheckBox checkBoxes;
}
@Override
public Filter getFilter() {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return obj.size();
}
@Override
public Object getItem(int position) {
return obj.get(position);
}
}
I'm struggling with integrating this adapter too, but I noticed the static keyword is missing from your holder class. Each time you call holder, it may not be what you think.
Take a look at this example. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
精彩评论