【问题标题】:Android ListView Vanishing ImagesAndroid ListView 消失的图像
【发布时间】:2010-11-04 02:09:06
【问题描述】:

我有一个 ListView,我正在为我手机上的每个联系人填充名称和图像(如果存在)。当应用程序最初加载时,这工作正常,但是一旦我开始滚动 ListView,原本正确的图像就会消失。我能想到的唯一一件事是资源被使用然后在它们滚出屏幕后被销毁,但我没有找到如何保留它们的运气。

我正在遍历我的所有联系人并存储姓名并使用 ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), photoUri);检索联系人图像的 InputStream。然后,在我的自定义 ArrayAdapter 中,我使用 Drawable.createFromStream() 为我的 ListView 项的 ImageView 设置图像。

谢谢!

编辑: 根据要求,这是我的 getView 方法

public View getView(int position, View convertView, ViewGroup parent)
{
    LinearLayout contact_view;
    //Get the current alert object
    ContactInfo contact = getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        contact_view = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, contact_view, true);
    }
    else
    {
        contact_view = (LinearLayout) convertView;
    }

    //Get the fields to populate from the listitem.xml file
    TextView contact_name = (TextView)contact_view.findViewById(R.id.contact_name);
    ImageView contact_image =(ImageView)contact_view.findViewById(R.id.contact_image);

    //Assign the appropriate data from our alert object above
    contact_name.setText(contact.get_name());
    if(contact.get_contact_image() != null) {
        contact_image.setImageDrawable(Drawable.createFromStream(contact.get_contact_image(), "src"));
    } else {
        contact_image.setImageResource(R.drawable.dummy_image);
    }

    return contact_view;
}

【问题讨论】:

  • 能否在帖子中包含适配器 getView() 方法的代码?

标签: android image listview android-2.2-froyo


【解决方案1】:

一旦您从 InputStream 中读取数据,您就会到达它的末尾,并且如果不向提供者请求新的 InputStream,就无法重新读取它。

【讨论】:

  • 谢谢,我今天下午想通了!我将 InputStream 读入位图,效果很好。
猜你喜欢
  • 2011-05-24
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
相关资源
最近更新 更多