在RecyclerView中使用他们的URL显示包含图像的ParseFile

时间:2015-09-17 00:41:09

标签: android parse-platform adapter android-recyclerview picasso

我在Parse.com上的表格中存储了三张图片,我想使用他们的网址显示这三张图片。

我检索了三张图片的网址,它们显示在RecyclerView中。您可以在我的屏幕截图中看到这一点。

那么如何使用他们的URL显示这三个图像?

我的主要活动:

public class RecyclerViewActivity extends Activity {

private List<Person> persons;
private RecyclerView rv;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.recyclerview_activity);

    rv=(RecyclerView)findViewById(R.id.rv);

    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);

    initializeData();
    initializeAdapter();
}

private void initializeData(){
    persons = new ArrayList<>();

    //Query to load String data into the card view/recycler view
    ParseQuery<ParseObject> query = ParseQuery.getQuery("events");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {

                for (int i = 0; i < objects.size(); i++) {



                    persons.add(new Person(objects.get(i).getString("name"), objects.get(i).getString("shortinfo"), objects.get(i).getParseFile("image"), img[i]));

                }
            } else {
                // something went wrong
            }


            initializeAdapter();
        }
    });

     }

private void initializeAdapter(){
    RVAdapter adapter = new RVAdapter(persons);
    rv.setAdapter(adapter);
}
}

MY ADAPTER:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

public static class PersonViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView personName;
    TextView personAge;
    ParseImageView personPhoto;
    TextView personPhoto1;

    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        personName = (TextView)itemView.findViewById(R.id.person_name);
        personAge = (TextView)itemView.findViewById(R.id.person_age);
        personPhoto = (ParseImageView)itemView.findViewById(R.id.person_photo);
        personPhoto1 = (TextView)itemView.findViewById(R.id.person_photo1);

    }
}

List<Person> persons;

RVAdapter(List<Person> persons){
    this.persons = persons;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    personViewHolder.personName.setText(persons.get(i).name);
    personViewHolder.personAge.setText(persons.get(i).age);
    personViewHolder.personPhoto.setParseFile(persons.get(0).photoId);
    personViewHolder.personPhoto1.setText(persons.get(i).photoId1);
    //personViewHolder.personPhoto.setParseFile(persons.get(2).photoId);


}

@Override
public int getItemCount() {
    return persons.size();
}
}

我的数据包含我的回收者视图数据:

class Person {
String name;
String age;
ParseFile photoId;
String photoId1;

Person(String name, String age, ParseFile photoId,String photoId1) {
    this.name = name;
    this.age = age;
    this.photoId = photoId;
    this.photoId1 = photoId1;
}
}

2 个答案:

答案 0 :(得分:3)

我会告诉你一个例子。这个例子将依赖于使用Picasso(Glide也可以!)因为您可能需要为大量图像执行此操作,并且使用Picasso可以节省时间。

首先,通过Gradle添加picasso:

compile 'com.squareup.picasso:picasso:2.5.2'

其次,对单个图像执行以下示例:

ParseFile image = persons.get(i).getParseFile("imageName");
Uri imageUri = Uri.parse(image.getUrl());
Picasso.with(context).load(uri.toString()).into(imageView);

请注意这是多么容易,并且由于您正在保存Parse,因此请使用适当的命名(即image_01234.jpg)保存文件,其中结束位是时间戳或您拥有的内容。

答案 1 :(得分:1)

我建议您使用像Picasso,Glide,Fresco,ImageRequest(Volley)这样的库,它具有许多功能,可以帮助您完成此任务。

properties

您应该使用imageView

Picasso.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);

Glide.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);

您的适配器

Glide.with(context).load("your_link_from_parse").into(yourImageView);

不要忘记在您的proyect中导入库。

查看此链接http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en