将数据从Parse.com加载到ListView中

时间:2012-11-25 20:07:36

标签: android listview parse-platform

我一直在使用Parse.com作为我在Android上的照片共享应用程序,但是我在列表视图中显示图像和数据时遇到了一些问题。

基本上我想从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中显示数据 - 但我的活动仍然是黑色。

有没有人有什么想法?

稍后更新:

这是我的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;
}

}

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:0)

我认为您的适配器出错了

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;

}

您正在检查视图是否为空。请仔细检查。