ImageView仅显示在listview的最后一行

时间:2017-08-25 21:58:55

标签: java android listview imageview

我有一个listview的活动,其中包含几个EditText和图像视图。 当用户注册他的数据(名称,电话,个人资料图像等...)存储在firebase中。 我希望listview行中的imageview显示每个用户的个人资料图像。 我有图像下载Uri,我使用picaso将配置文件图像设置为图像视图。

问题是:个人资料图片仅显示在列表视图的最后一行..

这是我将配置文件图像设置为ImageView的方式:

public void getProfileImage() {
    mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    User user = ds.getValue(User.class);
                    if (!user.getUserImage().equals("default"))
                        Picasso.with(getContext()).load(user.getUserImage()).into(profileImage);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

希望有人能弄明白问题是什么。 谢谢。

修改 这是listview适配器:

public class mainScreenAdapter extends ArrayAdapter<TrempData>  {

    private TextView  name ,timestamp ,uid, from,to,date,time,myextra ;
    private ImageButton phoneBtn;
    private ImageView timeline, profileImage;
    private int layoutResource;
    private FirebaseAuth firebaseAuth;
    private DatabaseReference mDatabase;



    public mainScreenAdapter(Context context, int layoutResource, ArrayList<TrempData> list) {
        super(context, layoutResource, list);
        this.layoutResource = layoutResource;
        firebaseAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance().getReference();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;


        if (view == null) {
            final LayoutInflater Inflater = LayoutInflater.from(getContext());
            view = Inflater.inflate(layoutResource, null);

        }

        final TrempData data = getItem(position);
        //data.setPos(position);
        //int[] androidColors = getContext().getResources().getIntArray(R.array.androidcolors);
        //int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];


        if (data != null) {
            name = (TextView) view.findViewById(R.id.myname);
            phoneBtn = (ImageButton) view.findViewById(R.id.myphone);
            //myphonestr = (TextView)view.findViewById(R.id.myphonestr);
            timestamp = (TextView) view.findViewById(R.id.mytimestamp);
            uid = (TextView) view.findViewById(R.id.myuid);
            timeline = (ImageView)view.findViewById(R.id.timeline);
            profileImage = (ImageView)view.findViewById(R.id.mycar);
            date = (TextView) view.findViewById(R.id.mydate);
            time = (TextView) view.findViewById(R.id.mytime);
            from = (TextView) view.findViewById(R.id.myfrom);
            to = (TextView) view.findViewById(R.id.myto);
            myextra = (TextView) view.findViewById(R.id.myextra);
            name.setText(data.get_name());
            //name.setTextColor(randomAndroidColor);
            //myphonestr.setText(data.get_phone());
            timestamp.setText(data.get_timestamp());
            uid.setText(data.get_uid());
            date.setText(data.get_date());
            time.setText(data.get_time());
            to.setText(data.get_to());
            from.setText(data.get_from());
            uid.setText(data.get_uid());
            myextra.setText(data.get_extras());

            phoneBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.get_phone()));
                    getContext().startActivity(i);
                }
            });

            if(firebaseAuth.getCurrentUser().getUid().equals(uid.getText())){

                phoneBtn.setVisibility(View.GONE);
                //car.setColorFilter(Color.rgb(255,164,30));

            }
            else{

                phoneBtn.setVisibility(View.VISIBLE);
                //car.setColorFilter(Color.rgb(176,176,176));

            }
        }
        getProfileImage();
        return view;

    }

    public void getProfileImage() {
        mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    User user = ds.getValue(User.class);
                    if (!user.getUserImage().equals("default"))
                        Picasso.with(getContext()).load(user.getUserImage()).into(profileImage);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

更新 我做了@Danilo de Oliveira建议我做的事情并且它有效但现在我遇到了新问题。尽管下载图像Uri在firebase中的每个用户中是不同的,但每个行中的配置文件图像相同

2 个答案:

答案 0 :(得分:1)

问题是每次getView()运行时都调用getProfileImage()。这就是实际发生的事情:

对于第1行:     循环遍历所有dataSnapshot.getChildren()          将图像加载到profileImage

对于第2行:     循环遍历所有dataSnapshot.getChildren()          将图像加载到profileImage

第3行:     循环遍历所有dataSnapshot.getChildren()          将图像加载到profileImage

...

因此,您总是将N张照片加载到profileImage,最后以最后一张图片显示每一行。

这应该是流程:

1. Load all your data from firebase.
2. Store them in an ArrayList<User> mUsers
3. listView.setAdapter(yourAdapter)
4. In getView()
    4a. Get your user by mUsers.get(position)
    4b. Get your imageview by view.findViewById(...)
    4c. Load your image here.

这应该有效。

另外我认为您应该检查列表视图和列表适配器的工作方式。

答案 1 :(得分:0)

“profileImage”是一个类变量,您可以通过异步请求获取图像uri,因此在加载图像uri时,您最后一次引用ImageView。 你可以试试......

// The key type Integer is an example
private HashMap<Integer, ImageView> imageMap = new HashMap<>();
private int trempDataListSize;

public mainScreenAdapter(Context context, int layoutResource, ArrayList<TrempData> list) {
    super(context, layoutResource, list);
    this.layoutResource = layoutResource;
    firebaseAuth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance().getReference();
    trempDataListSize = list.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ...
    TrempData data = getItem(position);
    ...
    ImageView profileImage = (ImageView) view.findViewById(R.id.mycar);
    ...
    // Store the userId as key and imageView as value
    imageMap.put(data.getUserId(), profileImage);
    ...
    // Execute getProfileImage() once
    if (position = trempDataListSize - 1) {
        getProfileImage();
    }
}

public void getProfileImage() {
    mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                User user = ds.getValue(User.class);

                // Check if imageMap has the key (userId)
                if (!user.getUserImage().equals("default")
                      && imageMap.containsKey(user.getId()))

                    // Get the value (imageView) by userId
                    Picasso.with(getContext())
                        .load(user.getUserImage())
                        .into(imageMap.get(user.getId()));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}