【问题标题】:Load Image Bitmap in Simple Adapter and show in Listview Android在 Simple Adapter 中加载图像位图并在 Listview Android 中显示
【发布时间】:2017-03-14 12:00:26
【问题描述】:

我在 Android Activity 中工作,我有列表视图并从 json 中的服务器动态加载数据,对于图像,我正在获取位图。

我卡住的地方是在 Listview 中加载位图图像我如何将图像集成到简单的适配器中

这是我的代码:

单行 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="@dimen/activity_horizontal_margin">

<TextView
    android:id="@+id/statusp"
    android:text="status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:layout_marginLeft="82dp"
    android:textColor="@color/colorAccent"
    android:layout_below="@+id/dspecs" />

<TextView
    android:id="@+id/dspecs"
    android:text="speciality"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:layout_marginLeft="82dp"
    android:textColor="@color/colorAccent"
    android:layout_below="@+id/usr_dname" />





<TextView
    android:id="@+id/usr_dname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="20sp"
    android:text="Name"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/imug_p1"
    android:layout_marginStart="41dp" />

<ImageView
    android:id="@+id/im2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/arrow"/>

<ImageView
    android:id="@+id/imug_p1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/hdoc"
    android:layout_above="@+id/statusp"
    android:layout_alignParentStart="true" />

我在这里添加数据

 if(singleUser.has("picture")){

                    pic = singleUser.get("picture").toString();

                    byte[] decodedString = Base64.decode(pic, Base64.DEFAULT);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);


                 Drawable did = new BitmapDrawable(getResources(), decodedByte);



                }




     HashMap<String, String> docs = new HashMap<>();

                        docs.put("name", key);

                        docs.put("speciality",speci);

                        docs.put("status", status);

                        docs.put("picture",did.toString()); ///here i am adding bitmap

                        contactList.add(docs);

这是一个简单的适配器

  ListAdapter adapter = new SimpleAdapter(Users.this, contactList,R.layout.user_listitem,
                    new String[]{"picture","name","speciality" ,"status"},
                    new int[]{R.id.imug_p1,R.id.usr_dname,R.id.dspecs,R.id.statusp});
            usersList.setAdapter(adapter);
            ((BaseAdapter) adapter).notifyDataSetChanged();

但列表视图中没有显示任何内容。

已编辑

我已经将位图转换为可绘制的,即使我看不到任何图像检查我更新的代码。

【问题讨论】:

  • 我已经检查过了,lazyadapter 类没有解决错误
  • 请将适配器代码作为文本发布。您现在无法引用它。
  • 在代码中查看我的编辑适配器
  • 谢谢。但是您现在已更改为R.id.imug_p。没关系。在你有一个drawable之前。

标签: android listview bitmap


【解决方案1】:

您需要创建自己的 BaseAdapter 子适配器。这是唯一的解决方案

【讨论】:

  • 我可以尝试使用自定义适配器吗?但后来我的数据也没有显示,我想我没有匹配正确的参考。通过查看我上面的代码,你能给出任何修改过的代码示例我应该如何使用自定义适配器
  • 你知道recyclerview吗?
  • 是的,它比使用 recuclerview 比使用 listview 更好,但我是新手,我不知道如何编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2018-08-20
  • 1970-01-01
  • 2012-09-17
相关资源
最近更新 更多