Android BaseAdapter中的notifyDataSetChanged()
这是我的代码,我试图从Arraylist中删除该项,并尝试使用notifyDataSetChanged更新布局项。Android BaseAdapter中的notifyDataSetChanged(),android,Android,这是我的代码,我试图从Arraylist中删除该项,并尝试使用notifyDataSetChanged更新布局项。 public class GroupContactListingAdapter extends BaseAdapter { private LayoutInflater mInflater; private ArrayList<ContactListDto> groupListDto = new ArrayList<ContactListDto&
public class GroupContactListingAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<ContactListDto> groupListDto = new ArrayList<ContactListDto>();
private Activity context;
public static ArrayList<String> selectList;
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
/**
* Assigning the parameter
*
* @param mContext1
* Getting the context of the class
* @param contactList
* list to be display
* @param value
* if 0 all, if 1 unblocked and 2 blocked
* @param isContactList
* if 1 then contact list and if 0 search contact list
*/
public GroupContactListingAdapter(Activity mContext1,
ArrayList<ContactListDto> contactList) {
this.context = mContext1;
this.mInflater = (LayoutInflater) mContext1
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (this.groupListDto != null) {
this.groupListDto.clear();
}
this.groupListDto = contactList;
selectList = new ArrayList<String>();
}
@Override
public int getCount() {
return groupListDto.size();
}
@Override
public Object getItem(int position) {
return groupListDto.get(position);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_listing_item, null);
viewHolder = new ViewHolder();
viewHolder.nameTextView = (TextView) convertView
.findViewById(R.id.group_name);
viewHolder.relative = (RelativeLayout) convertView
.findViewById(R.id.group_layout);
viewHolder.deleteContacts = (Button) convertView
.findViewById(R.id.btn_group_contact_delete);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.deleteContacts.setVisibility(View.VISIBLE);
viewHolder.deleteContacts
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.printLogD(" deleting position is " + position);
selectList.add(groupListDto.get(position)
.getContactId());
groupListDto.remove(groupListDto.get(position));
notifyDataSetChanged();
}
});
viewHolder.nameTextView.setText(groupListDto.get(position)
.getContactFullName());
if (position % 2 == 0) {
viewHolder.relative.setBackgroundColor(context.getResources()
.getColor(R.color.white));
} else {
viewHolder.relative.setBackgroundColor(context.getResources()
.getColor(R.color.list_even_color));
}
return convertView;
}
convertView .setTag(viewHolder);
试试这种代码可能会对你有所帮助
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
ViewHolder mViewHolder;
if(convertView == null){
mViewHolder = new ViewHolder();
mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.group_listing_item, null);
mViewHolder. nameTextView = (TextView) convertView .findViewById(R.id.group_name);
mViewHolder.relative = (RelativeLayout) convertView .findViewById(R.id.group_layout);
mViewHolder.deleteContacts = (Button) convertView .findViewById(R.id.btn_group_contact_delete);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder)convertView.getTag();
}
deleteContacts.setVisibility(View.VISIBLE);
deleteContacts .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.printLogD(" deleting position is " + position);
selectList.add(groupListDto.get(position) .getContactId());
groupListDto.remove(groupListDto.get(position));
notifyDataSetChanged();
}
});
nameTextView.setText(groupListDto.get(position) .getContactFullName());
if (position % 2 == 0) {
relative.setBackgroundColor(context.getResources() .getColor(R.color.white));
} else {
relative.setBackgroundColor(context.getResources() .getColor(R.color.list_even_color));
}
return convertView;
}
public class ViewHolder{
TextView nameTextView;
RelativeLayout relative;
Button deleteContacts;
}
造成此问题的原因是我没有在convertView实例中标记viewHolder。
public class GroupContactListingAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<ContactListDto> groupListDto = new ArrayList<ContactListDto>();
private Activity context;
public static ArrayList<String> selectList;
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
/**
* Assigning the parameter
*
* @param mContext1
* Getting the context of the class
* @param contactList
* list to be display
* @param value
* if 0 all, if 1 unblocked and 2 blocked
* @param isContactList
* if 1 then contact list and if 0 search contact list
*/
public GroupContactListingAdapter(Activity mContext1,
ArrayList<ContactListDto> contactList) {
this.context = mContext1;
this.mInflater = (LayoutInflater) mContext1
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (this.groupListDto != null) {
this.groupListDto.clear();
}
this.groupListDto = contactList;
selectList = new ArrayList<String>();
}
@Override
public int getCount() {
return groupListDto.size();
}
@Override
public Object getItem(int position) {
return groupListDto.get(position);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_listing_item, null);
viewHolder = new ViewHolder();
viewHolder.nameTextView = (TextView) convertView
.findViewById(R.id.group_name);
viewHolder.relative = (RelativeLayout) convertView
.findViewById(R.id.group_layout);
viewHolder.deleteContacts = (Button) convertView
.findViewById(R.id.btn_group_contact_delete);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.deleteContacts.setVisibility(View.VISIBLE);
viewHolder.deleteContacts
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.printLogD(" deleting position is " + position);
selectList.add(groupListDto.get(position)
.getContactId());
groupListDto.remove(groupListDto.get(position));
notifyDataSetChanged();
}
});
viewHolder.nameTextView.setText(groupListDto.get(position)
.getContactFullName());
if (position % 2 == 0) {
viewHolder.relative.setBackgroundColor(context.getResources()
.getColor(R.color.white));
} else {
viewHolder.relative.setBackgroundColor(context.getResources()
.getColor(R.color.list_even_color));
}
return convertView;
}
convertView .setTag(viewHolder);
代码中哪一行是94?您不能添加mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);在getview中,这就是发生此错误的原因。但它给出了nullpointer错误,因为构造函数只进行了一次初始化,但getview方法每次初始化直到行计数,这就是为什么要放入getview并查看结果