网格图像适配器排序

时间:2018-09-23 07:04:01

标签: java android arrays sorting gridview

我用图像构建了一些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 个答案:

答案 0 :(得分:0)

@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);