【发布时间】:2014-02-26 17:44:41
【问题描述】:
我在onCreate()中给ListView绑定了一个adapter,每次在onResume()中activity时,我都会更新adapter中的appinfos数据,并在adapter setdata()方法中调用notifyDataSetChanged。但是即使我的适配器中的数据发生了变化,我也没有刷新 ListView。我不知道是什么问题,你能帮帮我吗?谢谢
这是我使用适配器并将数据设置为列表视图的活动。
List<AppInfo> appinfos = new ArrayList<AppInfo>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getAppInfos();
adapter=new UnistallListAdapter(this,appinfos);
applistView.setAdapter(adapter);
}
@Override
protected void onResume() {
getAppInfos();
adapter.setdata(appinfos);
super.onResume();
}
private List<AppInfo> getAppInfos() {
appinfos.clear();
//do some thing, new an item; and add into appinfos object
//the appinfos data often changed here,
// as I uninstall app before activity called onResume().
......
appinfos.add(info);
.....
return appinfos;
}
下面是我的 UnistallListAdapter 的主要代码,以及如何更改它的数据。
private List<AppInfo>infos;
public UnistallListAdapter(Context c,List<AppInfo>info)
{
mContext = c;
inflate=LayoutInflater.from(c);
infos=info;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return infos.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return infos.size();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHold holder = null;
if(convertView==null)
{
convertView=inflate.inflate(R.layout.uninstall_item, null);
holder=new ViewHold();
holder.name=(TextView)convertView.findViewById(R.id.name);
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
holder.ai = infos.get(position);;
convertView.setTag(holder);
}
else
{
holder=(ViewHold)convertView.getTag();
}
AppInfo ai = holder.ai;
holder.name.setText(ai.mAppLabel);
holder.icon.setImageDrawable(ai.mIcon);
return convertView;
}
private class ViewHold
{
AppInfo ai;
ImageView icon;
TextView name;
}
public void setdata(List<AppInfo> dataList) {
infos = dataList;
notifyDataSetChanged();
}
请给我一些帮助,欢迎任何关于原因的提示。非常感谢您的帮助。
【问题讨论】:
-
请发布更多您的适配器代码。
-
我认为如果我们专注于更改的数据就足够了。你想看哪一部分?另一部分代码与实现getView、getItem、getCount等流程是一样的……
-
为什么在onResume()中调用update的东西?
-
@pskink,我在 onResume()->setdata()->notifyDataSetChanged(update stuff method) 中调用了 update stuff,我明白你的意思了吗?
-
@gladman 我在问为什么 onResume
标签: android listview android-listview