0

我一直在将 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;
}

}

4

2 回答 2

5

抱歉迟到了,但我觉得这可能有用 -
http://www.androidbegin.com/tutorial/android-parse-com-listview-images-and-texts-tutorial/

于 2013-09-23T18:40:43.650 回答
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;

}

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

于 2013-10-14T06:04:41.263 回答