【问题标题】:Loading data from Parse.com into ListView将 Parse.com 中的数据加载到 ListView
【发布时间】:2012-11-25 20:07:35
【问题描述】:

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

}

【问题讨论】:

  • 向我们展示你的惰性适配器类
  • 首先,contentForList.add(infoHashMap); 行不应该在那个for 循环中,所以你添加所有Maps,而不是添加一个带有最后一个值的Map?其次,您是否检查过您传递给适配器的contentForList 列表中是否有一些值?第三,ListViewActivity 布局中唯一的小部件(如果不是,您介意添加该布局文件)吗?

标签: android listview parse-platform


【解决方案1】:

【讨论】:

    【解决方案2】:

    我认为你的适配器出了问题

    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;
    

    }

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-21
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多