【问题标题】:Hide Image in Custom ListView In Android在Android中的自定义ListView中隐藏图像
【发布时间】:2013-11-17 19:47:35
【问题描述】:

我正在为社交网站设计一个 Android 应用程序。 我想显示用户朋友发布的帖子, 可以有两种类型的帖子,照片帖子或纯文字帖子, 我创建了一个自定义列表视图,它以 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);
            } 
}

当我执行它时,所有帖子图片都变得不可见

【问题讨论】:

  • 发布适配器的代码(或您正在使用的相关代码)。
  • 我编辑了问题并放置了代码,如果可能的话,请看看n给我解决方案

标签: android listview


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多