我有一个用于管理 ListView 的自定义 ArrayAdapter:
public class FilesAdapter extends ArrayAdapter<PutioFileLayout> {
Context context;
int layoutResourceId;
PutioFileLayout data[] = null;
public FilesAdapter(Context context, int layoutResourceId, PutioFileLayout[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
FileHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new FileHolder();
holder.textName = (TextView) row.findViewById(R.id.text_fileName);
holder.textDescription = (TextView) row.findViewById(R.id.text_fileDesc);
holder.imgIcon = (ImageView) row.findViewById(R.id.img_fileIcon);
row.setTag(holder);
} else {
holder = (FileHolder) row.getTag();
}
PutioFileLayout file = data[position];
holder.textName.setText(file.name);
holder.textDescription.setText(file.description);
holder.imgIcon.setImageResource(file.icon);
return row;
}
static class FileHolder {
TextView textName;
TextView textDescription;
ImageView imgIcon;
}
}
在我的片段中,我在以下声明之前声明了这些onCreate
:
private PutioFileLayout[] fileData;
private FilesAdapter adapter;
然后,在 中onCreate
,我用它初始化它,作为用户的占位符:
fileData = new PutioFileLayout[] {
new PutioFileLayout("Loading...", "Your files will appear shortly.", R.drawable.ic_launcher, false)
};
我初始化我的适配器:
adapter = new FilesAdapter(getSherlockActivity(), R.layout.file,
fileData);
listview.setAdapter(adapter);
后来,在一些网络工作之后,我更新了适配器的数据。这就是我认为我可能会出错的地方。
PutioFileLayout[] files = blah blah, stuff. I've confirmed in debug that this array is correct and has the correct number of values.
adapter.data = files;
adapter.notifyDataSetChanged();
所有这些的结果是 ListView 显示数组中的第一个项目,仅此而已。
如果我在初始化时添加第二个 Loading... 行或类似行,它会显示前两个。等等。
我错过了什么吗?这样做的正确方法是什么?