我一直在使用Parse.com为我的照片共享应用程序在Android上,但我有一些问题,在列表视图中显示图像和数据。从Parse.com将数据加载到ListView中
基本上我想从Parse.com获取数据并将其显示在listView中。
在我的MainActivity中,我有方法downloadContentForMainView() - 我获取我的数据并将其插入到HashMap中 - 并且在获取所有数据后,我使用适配器在ListView中显示数据。
这里是我的代码:
private void downloadContentForMainView() throws ParseException
{
ParseQuery query = new ParseQuery("PhotoUpload");
query.whereEqualTo("User", username);
query.findInBackground(new FindCallback() {
public void done(List<ParseObject> content, ParseException pEx) {
// TODO Auto-generated method stub
if(pEx == null && content!=null)
{
if(!(content.isEmpty()))
{
if((content!=null) && (!(content.isEmpty())))
{
for(ParseObject aParseObject : content)
{
ParseFile image = (ParseFile) aParseObject.get("photp");
image.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] imageInBytes, ParseException pEx) {
// TODO Auto-generated method stub
bmp = BitmapFactory.decodeByteArray(imageInBytes, 0, imageInBytes.length);
}
});
String objectId = aParseObject.getObjectId();
String date = aParseObject.getCreatedAt().toGMTString();
infoHashMap.put("objectId", objectId);
infoHashMap.put("Date", date);
infoHashMap.put("photo", bmp);
}
}
else
{
Toast toast2 = Toast.makeText(context,"Couldn't fetch data from server", Toast.LENGTH_LONG);
toast2.show();
}
}
}
}
});
contentForList.add(infoHashMap);
lazyAdapter = new LazyAdapter(this,contentForList);
listView.setAdapter(lazyAdapter);
}
我取出后从服务器我所有的数据 - 我使用适配器显示在ListView数据 - 但我的活动依然为黑色。
有没有人有任何ideea为什么?
后来更新:
这里是我的LazyAdapter
public class LazyAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String, Object>> contentList;
private static LayoutInflater inflater = null;
public LazyAdapter(Activity a, ArrayList<HashMap<String, Object>> contentForList)
{
this.activity=a;
this.contentList=contentForList;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return contentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
ImageView logo = (ImageView)vi.findViewById(R.id.logo); // title
TextView date = (TextView)vi.findViewById(R.id.label); // artist name
HashMap<String, Object> info = new HashMap<String,Object>();
info = contentList.get(position);
String in =(String)info.get("Date");
// Setting all values in listview
date.setText(in);
Bitmap bmp = (Bitmap) info.get("photo");
logo.setImageBitmap(bmp);
return vi;
}
}
向我们展示您的懒适配器类 – mango
首先,不应该在''contentForList.add(infoHashMap);'在该'for'循环中添加所有'Maps',而不是添加一个'Map'最后的值?其次,你是否检查过你传递给适配器的'contentForList'列表中是否有一些值?第三,'ListView'是'Activity'布局中唯一的小部件(如果你不介意添加布局文件)? – Luksprog