简体   繁体   English

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

[英]Hide Image in Custom ListView In Android

I am designing a Android Application For A Social Networking Site. 我正在为社交网站设计一个Android应用程序。 i want to display the posts which users friend Posted, There can be 2 types of post, a photo post or a text only post, i created a Custom listview which gets the data in JSON format. 我想显示用户朋友发布的帖子,可以有2种类型的帖子,照片帖子或纯文本帖子,我创建了一个Custom listview,它以JSON格式获取数据。 the structure of Custom List view is 自定义列表视图的结构是

Friend Name (textView ) Posted Picture (ImageView ) Posted Date & Time 朋友姓名(textView)发布图片(ImageView)发布日期和时间

Now The Issue is , i am not able to hide the Imageview Control For the Text only post as there is no need of image here. 现在的问题是,我无法隐藏“仅文本”的Imageview控件,因为这里不需要图像。

when i try to hide the image for textonly posts, all the images for all the posts gets invisible. 当我尝试隐藏纯文本帖子的图像时,所有帖子的所有图像都变得不可见。

Please help me to achieve this. 请帮助我实现这一目标。

Here Is The Code 这是代码

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

When I execute it, all the post pictures gets invisible 当我执行它时,所有帖子图片都变得不可见

You need to set the ImageView to visible when you want to display items otherwise it will always stay invisible: 要显示项目时,需要将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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM