无法在listview中显示parse.com中的图像

时间:2015-08-29 13:21:37

标签: android listview parse-platform bitmap android-image

我正在尝试检索我在Parse.com上保存的图像。没有错误,但图像没有显示。我正在使用自定义适配器。

这是活动。我尝试运行调试器但在那里找不到任何问题。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_activty);

    mProgress = (ProgressBar)findViewById(R.id.progressBar2);
    lv = (ListView)findViewById(R.id.listView);
    fileArrayList =  new ArrayList<ParseFile>();

    mProgress.setVisibility(View.VISIBLE);


    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Images");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                mProgress.setVisibility(View.INVISIBLE);
                for (ParseObject obj : list) {
                    ParseFile i = obj.getParseFile("Image");
                    fileArrayList.add(i);
                }
            } else {
                Toast.makeText(getApplicationContext(), "Error retrieving images: " + e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        }
    });

    listviewAdapter = new ListviewAdapter(ImageActivty.this, fileArrayList);
    lv.setAdapter(listviewAdapter);


}

这是我的适配器。

public class ListviewAdapter extends BaseAdapter{

private final static String TAG = ListviewAdapter.class.getSimpleName();

private Context activity;
private LayoutInflater inflater = null;
private List<ParseFile> sellers;
int layout;

public ListviewAdapter(Context activity, List<ParseFile> sellers) {

    this.activity = activity;
    this.sellers = sellers;
    inflater = LayoutInflater.from(activity);
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public  class ViewHolder {
    ImageView imageView;
    FrameLayout rl;
    TextView textView;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    View v =view;
    ViewHolder holder = new ViewHolder();

    if (view == null) {
        LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.list_item,null);
        holder.imageView = (ImageView)view.findViewById(R.id.imageView2);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }


    ParseFile s = sellers.get(position);
    byte[] bitmapdata = new byte[0];
    try {
        bitmapdata = s.getData();
    } catch (ParseException e) {
        e.printStackTrace();
        Log.d("ListAdapter","Error here.");
    }
    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata.length);

    holder.imageView.setImageBitmap(bitmap);
    Log.d("CustomAdapter.class", "CustomAdapter");
    return v;
 }
}

我的一位朋友帮助我将parsefile转换为适配器中的位图。我怀疑实施是错误的。如果是,我如何将ParseFile转换为位图。有没有其他方法可以从Parse.com的列表中检索图像并将其设置为imageview?

2 个答案:

答案 0 :(得分:0)

实际上,这段代码:

listviewAdapter = new ListviewAdapter(ImageActivty.this, fileArrayList);
lv.setAdapter(listviewAdapter);

在此之前运行

 if (e == null) {
            mProgress.setVisibility(View.INVISIBLE);
            for (ParseObject obj : list) {
                ParseFile i = obj.getParseFile("Image");
                fileArrayList.add(i);
            }
        } else {
            Toast.makeText(getApplicationContext(), "Error retrieving images: " + e.getMessage(), Toast.LENGTH_SHORT).show();

        }

在大多数情况下,因为findInBackground是异步的。然后,一旦您的图片被加载,您必须通知您的适配器其数据集已更改:

 listViewAdapter.notifyDataSetChanged();

在这里做:

   if (e == null) {
            mProgress.setVisibility(View.INVISIBLE);
            for (ParseObject obj : list) {
                ParseFile i = obj.getParseFile("Image");
                fileArrayList.add(i);
            }
            listViewAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getApplicationContext(), "Error retrieving   images: " + e.getMessage(), Toast.LENGTH_SHORT).show();

        }

此时您还可以实例化并设置listAdapter。

答案 1 :(得分:0)

好。我自己想出来了。我不得不将ParseQuery和适配器移动到异步任务。这样我只有在后台任务完成时才能操作适配器。 Thibault提供的概念非常方便。另外,我使用通用图像加载器来执行图像设置。我也尝试过毕加索,它也很有用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_activty);

    new RemoteDataTask().execute();

}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(ImageActivty.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Images loading");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.getProgress();
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        worldpopulationlist = new ArrayList<ParseImage>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "Images");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            ob = query.find();
            for (ParseObject country : ob) {
                country.pinInBackground();
                // Locate images in flag column
                ParseImage parseImage = new ParseImage();
                ParseFile image = (ParseFile) country.get("Image");
                String objectID = country.getObjectId();
                parseImage.setImage(image);
                parseImage.setObjectID(objectID);
                worldpopulationlist.add(parseImage);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (GridView) findViewById(R.id.listView);
        // Pass the results into ListViewAdapter.java
        adapter = new ListviewAdapter(ImageActivty.this,
                worldpopulationlist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}