我想显示用户在活动中上传的随机图片,当他们点击它时会将它们带到用户页面。
我所做的是将这些图像上传到firebase存储中并创建了他们的数据库。然后我将这些图像URL存储在一个arraylist中,使用Collections.shuffle()将其随机化并将其传递给循环视图以显示这些图像。还尝试将这些图像与firebase数据库中的详细信息url相关联。我想知道这样做的更好方法是什么?
{
"dj" : {
"artist1" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"name" : "artistone",
"place" : "plac1",
"price" : 1000
},
"artist2" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"name" : "artisttwo",
"place" : "place2",
"price" : 20000
}
},
"randomimages" : {
"image1" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist1"
},
"image10" : {
"image" : "https://firebasestorage.googleapis.com/v0/",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist1"
},
"image2" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist2"
},
"image3" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist1"
},
"image4" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist2"
},
"image5" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist2"
},
"image6" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist1"
},
"image7" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist1"
},
"image8" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist2"
},
"image9" : {
"image" : "https://firebasestorage.googleapis.com/v0/b",
"vendorurl" : "https://xyz-.firebaseio.com/dj/artist2"
}
}
我的随机图像活动
public class RandomImages extends AppCompatActivity {
private static final String TAG = "RandomImages";
private RecyclerView recyclerView;
RandomImagesRecyclerViewAdapter adapter;
private Intent randomimagesintent;
private String RANDOMIMAGEVENDORURL="randomimagevendorurl";
public ArrayList<Integer> index=new ArrayList<>();
final static ArrayList<String> imagearray = new ArrayList<String>();
final static ArrayList<String> vendorurlarray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_images);
new RandomImagesAsyncTask().execute("");
}
private class RandomImagesAsyncTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://wedapp-86c5d.firebaseio.com/randomimages");
databaseReference.addValueEventListener(new ValueEventListener() {
public static final String TAG = "RandomImages";
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: firebase started");
for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {
RandomImagesRecyclerViewAdapter.Image image = imageSnapshot.getValue(RandomImagesRecyclerViewAdapter.Image.class);
imagearray.add(image.getImage());
vendorurlarray.add(image.getVendorurl());
Log.e(TAG, "onDataChange: exiting firebase on data change");
}
for(int x=0;x<imagearray.size();x++)
index.add(x);
addimages();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return null;
}
}
private void addimages() {
Log.e(TAG, "addimages: starting adapter");
recyclerView = (RecyclerView) findViewById(R.id.randomimages_recycleView);
Collections.shuffle(index); // randomize the display of images
Log.e(TAG, "addimages: "+imagearray );
adapter = new RandomImagesRecyclerViewAdapter(this, RandomImagesRecyclerViewAdapter.getData(imagearray, index));
recyclerView.setAdapter(adapter);
StaggeredGridLayoutManager l = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
// l.setOrientation(GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(l);
randomimagesintent = new Intent(RandomImages.this,ExpandedViewOfVendor.class);
recyclerView.addOnItemTouchListener(new MainActivityRecyclerViewItemClickListener(RandomImages.this, new MainActivityRecyclerViewItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(RandomImages.this, " " + position, Toast.LENGTH_SHORT).show();
randomimagesintent.putExtra(RANDOMIMAGEVENDORURL,vendorurlarray.get(index.get(position)));
startActivity(randomimagesintent);
}
}));
}
}
我的randomimagesadapter
public class RandomImagesRecyclerViewAdapter extends RecyclerView.Adapter<RandomImagesRecyclerViewAdapter.MyViewHolder> {
private Context context;
private ArrayList<Information> data;
public static ArrayList<String> vendorimagemap = new ArrayList<String>();
private LayoutInflater inflater;
public static final String TAG = "RIRVA";
static class Information {
public String image;
}
static class Image {
String image;
String vendorurl;
public Image() {
}
public Image(String image, String vendorurl) {
this.image = image;
this.vendorurl = vendorurl;
}
public String getImage() {
return image;
}
public String getVendorurl() {
return vendorurl;
}
}
public static ArrayList<Information> getData(ArrayList<String> imagearray, ArrayList<Integer> index) {
Log.e(TAG, "getData: entered function");
ArrayList<Information> data = new ArrayList<>();
for (int i = 0; i < imagearray.size(); i++) {
Information current = new Information();
current.image = imagearray.get(index.get(i));
data.add(current);
}
Log.e(TAG, "getData: leaving function");
return data;
}
public RandomImagesRecyclerViewAdapter(Context context, ArrayList<Information> data) {
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = inflater.inflate(R.layout.main_activity_recyclerview_cardview_singlerow, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {
Picasso.with(context).load(data.get(position).image).resize(612,612).into(myViewHolder.imageView);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.mainActivity_img_row);
}
}
}
答案 0 :(得分:0)
您可以使用以下方法随机化您的imageList:
Collections.shuffle(imageArray);