我一直在尝试使 ListView 显示图标以及基于从 Parse 提取的信息的图像。除了图像之外的所有内容都是在我的 BaseAdapter 内部使用时设置的。图像(位图)不会出现,仅显示空白区域。这是我到目前为止所做的:
MainActivity.java(扩展ListActivity)
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("username", "~");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(final List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
ArrayList myList = new ArrayList();
for (int i = 0; i < objects.size(); i++) {
ParseFile picture = objects.get(i).getParseFile("profilepicture");
picture.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if (e == null) {
if (data.length == 0) {
// data found, but nothing to extract. bad image or upload?
Toast.makeText(getApplicationContext(), "Image is bad. Please Try Again Later", Toast.LENGTH_LONG).show();
return;
}
// SUCCESS
// convert data and display in an imageview
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
} else {
Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_LONG).show();
}
}
});
FriendListData fld = new FriendListData();
fld.setUsername(objects.get(i).getUsername());
fld.setFullname(objects.get(i).getString("name"));
fld.setProfilePicture(bmp);
myList.add(fld);
lvDetail.setAdapter(new FriendBaseAdapter(context, myList));
FriendListData.java(只是位图代码)
public Bitmap getProfilePicture(){
return profilePicture;
}
public void setProfilePicture(Bitmap profilePicture){
this.profilePicture = profilePicture;
}
FriendBaseAdapter.java(getView()
和 detail()
)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.friend_list_item, null, true);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.fullname = detail(convertView, R.id.fullname, myList.get(position).getFullname());
mViewHolder.username = detail(convertView, R.id.username, myList.get(position).getUsername());
mViewHolder.profilePicture = detail(convertView, R.id.profilepicture, myList.get(position).getProfilePicture());
return convertView;
}
private ImageView detail(View v, int resId, Bitmap profilepicture){
ImageView imageView = (ImageView)v.findViewById(resId);
imageView.setImageBitmap(profilepicture);
return imageView;
}
如果需要,可以显示更多代码。我的代码没有遇到任何运行时错误,并且我能够从 Parse 中提取单词,所以我无法想象我做错了什么。感谢所有帮助!
我是一名优秀的程序员,十分优秀!