下面是我的适配器代码和日志(日志标签是“jieke”),问题是它有时无法在列表中显示整个项目,比如只显示8项,同时列表大小为10! 在日志中看到:getCount返回10,getView也调用了10次,但我仍然只看到8次查看,为什么?
public class NumberAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<NumberModel> mPhoneNumberList;
public NumberAdapter(Context context, ArrayList<NumberModel> phoneNumberList){
mInflater = LayoutInflater.from(context);
mPhoneNumberList = phoneNumberList;
}
@Override
public int getCount() {
if (mPhoneNumberList != null) {
LogUtils.d("jieke", "count="+mPhoneNumberList.size() );
return mPhoneNumberList.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (position > -1 && mPhoneNumberList != null && position < mPhoneNumberList.size()) {
return mPhoneNumberList.get(position);
}
return null;
}
public void setPhoneNumberList(ArrayList<NumberModel> phoneNumberList){
mPhoneNumberList = phoneNumberList;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LogUtils.d("jieke", "get view on position= " +position );
ItemViewTag viewTag;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item_custom_number, null);
viewTag = new ItemViewTag((ImageView) convertView.findViewById(R.id.item_image)
,(ImageView) convertView.findViewById(R.id.item_phone)
, (TextView) convertView.findViewById(R.id.item_text));
convertView.setTag(viewTag);
} else {
viewTag = (ItemViewTag) convertView.getTag();
}
return convertView;
}
class ItemViewTag {
protected ImageView mIcon;
protected ImageView mPhone;
protected TextView mName;
public ItemViewTag(ImageView icon, ImageView phone, TextView name){
this.mName = name;
this.mIcon = icon;
this.mPhone = phone;
}
}
}
这是Log:
05-13 21:20:24.975 D/absec.:CustomServiceNumberTabItemView(18651): reset adaptor this=1
05-13 21:20:24.975 D/absec.:jieke(18651): count=10
05-13 21:20:24.975 D/absec.:jieke(18651): count=10
05-13 21:20:24.975 D/absec.:CustomServiceNumberTabItemView(18651): notify change this=1
05-13 21:20:24.975 D/absec.:jieke(18651): count=10
05-13 21:20:24.975 D/absec.:jieke(18651): count=10
05-13 21:20:24.975 I/absec.:QBaseTabView(18651): onResume
05-13 21:20:24.985 D/absec.:jieke(18651): count=10
05-13 21:20:24.985 D/absec.:jieke(18651): get view on position= 0
05-13 21:20:24.985 D/absec.:jieke(18651): count=10
05-13 21:20:24.985 D/absec.:jieke(18651): get view on position= 0
05-13 21:20:24.985 D/absec.:jieke(18651): count=10
05-13 21:20:24.985 D/absec.:jieke(18651): get view on position= 0
05-13 21:20:24.985 D/absec.:jieke(18651): count=10
05-13 21:20:24.985 D/absec.:jieke(18651): get view on position= 0
05-13 21:20:25.005 D/absec.:jieke(18651): get view on position= 0
05-13 21:20:25.015 D/absec.:jieke(18651): get view on position= 1
05-13 21:20:25.015 D/absec.:jieke(18651): get view on position= 2
05-13 21:20:25.015 D/absec.:jieke(18651): get view on position= 3
05-13 21:20:25.015 D/absec.:jieke(18651): get view on position= 4
05-13 21:20:25.035 D/absec.:jieke(18651): get view on position= 5
05-13 21:20:25.035 D/absec.:jieke(18651): get view on position= 6
05-13 21:20:25.035 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.045 D/absec.:jieke(18651): get view on position= 7
05-13 21:20:25.045 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.055 D/absec.:jieke(18651): get view on position= 8
05-13 21:20:25.055 D/absec.:jieke(18651): get view on position= 9
05-13 21:20:25.065 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.075 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.085 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.095 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.105 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.115 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.125 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.135 D/absec.:CustomServiceActivity(18651): values=is_common=1
05-13 21:20:25.265 D/absec.:CustomServiceNumberAddActivity(18651): AddActivity destroy
实际上,我只需要在mPhoneNumberList更改后刷新gridView(在一些mPhoneNumberList.add()或mPhoneNumberList.remove()之后)但我发现使用NumberAdapter.notifyDataSetChanged()不起作用,所以每次我创建一个新的Adapter并且使用GridView.setAdapter(newNumberAdapter)重置GridView,它可以工作,但有时会发生问题。
在GridView.setAdapter(newNumberAdapter)之后问题解决了 添加此GridView.invalidateViews()适合我!
答案 0 :(得分:0)
我相信您正在使用 Asynctask 。在 doInBackground(字符串)中使用arraylist ArrayList<NumberModel> mPhoneNumberList
(您在适配器的构造函数中作为参数发送) asynctask的... params),如:mPhoneNumberList=new ArrayList<NumberModel>
,而不是在onCreate()中创建实例。
尝试解决方案。我相信您的适配器将会刷新。
答案 1 :(得分:0)
问题解决了 添加此GridView.invalidateViews()适合我!