我用圖像構建了一些 gridView,一切正常。我有一組圖像,我可以點擊它們中的每一個,然后根據需要繼續進行另一項活動。問題是 - 當我向適配器添加新圖像時 - 它被添加到堆棧的末尾。我怎樣才能重新排列圖像的顯示從新到舊而不是像現在一樣從舊到新。(我自己做了一些事情(正如你在評論中看到的),它的顯示就像我想要的,但是當我點擊圖片時,我得到了錯誤的圖片):final ArrayList<String> imgString = new ArrayList<>(); DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); Query query = reference .child("user_photos") .child(FirebaseAuth.getInstance().getCurrentUser().getUid()); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for ( DataSnapshot singleSnapshot : dataSnapshot.getChildren()){ // photos.add(singleSnapshot.getValue(Photo.class)); imgString.add(singleSnapshot.child("postimage").getValue().toString()); } //setup our image grid int gridWidth = getResources().getDisplayMetrics().widthPixels; int imageWidth = gridWidth/NUM_GRID_COLUMNS; gridView.setColumnWidth(imageWidth); final ArrayList<String> imgUrls = new ArrayList<String>(); for(int i = 0; i < imgString.size(); i++){ imgUrls.add(imgString.get(i));// for(int i = imgString.size()-1; i >=0 ; i--){// imgUrls.add(imgString.get(i)); } GridImageAdapter adapter = new GridImageAdapter(ProfileActivity.this,R.layout.layout_grid_imageview,"", imgUrls); gridView.setAdapter(adapter);
1 回答

陪伴而非守候
TA貢獻1757條經驗 獲得超8個贊
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PostKey=imgString.get(i);//here i is the position of the view in the adapter.
正如你所做的那樣:
for(int i = imgString.size()-1; i >=0 ; i--){
imgUrls.add(imgString.get(i));//imgUrls.get(0) stored imgString.get(imgString.size()-1)
當您單擊 0 位置的項目時,其 imgUrls 不指向imgString.get(0),而是imgString.get(imgString.size()-1)。
編輯:試試這個:
PostKey=imgString.get(imgString.size()-1-i);
添加回答
舉報
0/150
提交
取消