隐藏自定义ListView中的图像在Android中

时间:2013-11-17 19:47:35

标签: android listview

我正在设计一个社交网站的Android应用程序。 我想显示用户朋友发布的帖子, 可以有2种类型的帖子,照片帖子或仅文字帖子, 我创建了一个自定义列表视图,它以JSON格式获取数据。自定义列表视图的结构是

朋友姓名(textView) 张贴图片(ImageView) 发布日期&时间

现在问题是,我无法隐藏Imageview控件仅用于文本帖子,因为这里不需要图像。

当我尝试隐藏文字帖子的图片时,所有帖子的所有图片都不可见。

请帮助我实现这一目标。

这是守则

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if ( itemView == null)
            {
                itemView = getLayoutInflater().inflate(R.layout.postlistview, parent,false);
            }
            Posts CurrentPost = myPosts.get(position);
            // myPosts Is the List Of Posts, (Usermane, Posttext, PostImage, IsPhotoPost)

            //Retrieving The User Name
            TextView userName = (TextView) itemView.findViewById(R.id.post_username);
            userName.setText(CurrentPost.getUserName());

            // Retrieving Whether the Post Is TextOnly Or Photo Post and Photo AssignMent 
            ImageView imgView =(ImageView) itemView.findViewById(R.id.post_photo);
            if ( CurrentPost.IsPhotoPost() ) // If True
                {
                    URL url = null;
                    Bitmap bmp = null;
                    try {
                        url = new URL(CurrentPost.getPostPhoto());
                        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    } catch (MalformedURLException e) {

                    }catch (IOException e) {

                    }
                    imgView.setImageBitmap(bmp); 
                }
            else
            {
                imgView.setVisibility(View.INVISIBLE);
            } 
}

执行时,所有帖子图片都不可见

1 个答案:

答案 0 :(得分:1)

当您想要显示项目时,您需要将ImageView设置为可见,否则它将始终不可见:

ImageView imgView =(ImageView) itemView.findViewById(R.id.post_photo);
if ( CurrentPost.IsPhotoPost() ) // If True
    {
        URL url = null;
        Bitmap bmp = null;
        try {
            url = new URL(CurrentPost.getPostPhoto());
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (MalformedURLException e) {

        }catch (IOException e) {

        }
        imgView.setImageBitmap(bmp); 
        imgView.setVisibility(View.VISIBLE);
    }
else
{
    imgView.setVisibility(View.INVISIBLE);