如何在每个图像视图上设置单击侦听器

时间:2017-10-25 11:45:40

标签: android android-layout firebase-realtime-database android-imageview

我有一个子布局,并在此布局上设置了值事件监听器。现在,我想在每个布局的图像视图上设置单击侦听器,以便在不同的活动中打开此图像,但此处仅在每次单击时拾取最后一个图像。 我知道为什么会这样,但我无法将这个问题弄错。

secondValueListener=new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //notificationCount = 0;
        //have to call removerView() to remove the previous view;
        convertView = getLayoutInflater().inflate(R.layout.notice_item_layout, null);
        nameTV = ((TextView) convertView.findViewById(R.id.name));
        dateTV = ((TextView) convertView.findViewById(R.id.date));
        noticeTV = ((TextView) convertView.findViewById(R.id.notice));
        noticeImg=(ImageView) convertView.findViewById(R.id.noticeImg);
        ((ViewGroup) parentLayout).removeAllViews();
        UserBean userBean = ReferenceWrapper.getRefrenceWrapper(NoticeBoardActivity.this).getUserBean();
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(ParameterConstants.USERS);
          dbRef.child(ParameterConstants.PROFILE).child(userBean.getMobile()).setValue(userBean);
        if (dataSnapshot.getValue() != null) {
            Iterable<DataSnapshot> snapshotIterator = dataSnapshot.getChildren();
            Iterator<DataSnapshot> iterator = snapshotIterator.iterator();
            Collections.reverse(list);
            for (int i = 0; i < list.size(); i++) {
                convertView = getLayoutInflater().inflate(R.layout.notice_item_layout, null);
                nameTV = ((TextView) convertView.findViewById(R.id.name));
                dateTV = ((TextView) convertView.findViewById(R.id.date));
                noticeTV = ((TextView) convertView.findViewById(R.id.notice));
                noticeImg=(ImageView) convertView.findViewById(R.id.noticeImg);
                nameTV.setText(list.get(i).getUser());
                dateTV.setText(list.get(i).getTime());
                noticeTV.setText(list.get(i).getNoticeText());
                if (list.get(i).getImage().equals("")) {
                    noticeImg.setVisibility(View.GONE);
                    } else {
                    noticeImg.setVisibility(View.VISIBLE);
                    noticeImg.setImageBitmap(Conversion.BitMapfromString(list.get(i).getImage()));
                    noticeImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent=new Intent(NoticeBoardActivity.this,OpenNoticeImageActivity.class);
                            Bitmap bitmap = ((BitmapDrawable)noticeImg.getDrawable()).getBitmap();
                            byte[] byteArr=Conversion.byteArrayFromBitmap(bitmap);
                            intent.putExtra("imageBitmap",byteArr);
                            startActivity(intent);
                        }
                    });
                }
                ((ViewGroup) parentLayout).addView(convertView);
            }
        }
        progressDialog.dismiss();
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
};

0 个答案:

没有答案