代码之家  ›  专栏  ›  技术社区  ›  Dima Bokov

网格图像适配器排序

  •  0
  • Dima Bokov  · 技术社区  · 6 年前

    我用图像构建了一些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);
    
    
    
                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
                    {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            PostKey=imgString.get(i);
                            Intent intent = new Intent(ProfileActivity.this, ViewPostActivity.class);
                            intent.putExtra("PostKey_profile",PostKey);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.d(TAG, "onCancelled: query cancelled.");
                }
            });
        }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   navylover    6 年前
    @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)
    

    imgString.get(0) ,但是 imgString.get(imgString.size()-1) .

    试试这个:

    PostKey=imgString.get(imgString.size()-1-i);