【问题标题】:Android Studio: Dynamically add icon to ListViewAndroid Studio:动态添加图标到 ListView
【发布时间】:2018-03-07 20:46:33
【问题描述】:

试图自学如何做这些事情。我试图弄清楚如何将图像动态添加到 ListView 项。我正在做的是提取 JSON 数据,返回以下内容:

{"mdslist":
    [
        {
            "UID":"3",
            "name":"Stamford, CT",
            "date":"March 10, 2018",
            "icon":"badge_stamford"
        },
        {
            "UID":"4",
            "name":"Enschede, The Netherlands",
            "date":"March 11, 2018",
            "icon":"badge_enschede"
        }
    ]
}

然后处理该数据并将其转储到 ListView 中。 ListView 有单独的容器来保存数据 - 请注意 ImageView 标记中的临时 img:

md_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/md_item_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:minHeight="65dp">

    <ImageView
        android:id="@+id/md_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/md_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/badge_stamford" />

    <TextView
        android:id="@+id/md_name"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/md_icon"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/md_date"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:paddingBottom="2dip"
        android:textColor="@color/dark_gray"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/md_icon"
        app:layout_constraintTop_toBottomOf="@+id/md_name" />

</android.support.constraint.ConstraintLayout>

JSON 数据在处理后的 onPostExecute() 调用期间被注入:

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()) {
            pd.dismiss();
        }
        /**
         * Dump parsed JSON data into ListView
         **/
        ListAdapter adapter = new SimpleAdapter(
                md_list_all.this, mdList,
                R.layout.md_list_item, new String[]{"name", "date"}, new int[]{R.id.md_name, R.id.md_date});
        lv.setAdapter(adapter);
    }

如您所见,我无法添加图标数据。这些文件被命名为 JSON 数据,扩展名为 .jpg。我想要的是获取 JSON 的“图标”信息,并将其注入 ImageView。

【问题讨论】:

  • 如果这些图标在您的服务器端存储中,则使用图标名称附加图标的根 url 路径,并使用 GLIDE 或 Picasso 将 URL 加载到 imageView 中!
  • 他们不是。他们正在当地被拉进来。尽管您确实让我想知道我是否应该将它们放在服务器端并保持应用程序轻便。
  • 如果您将它们放在客户端,您将无法在不更新 Playstore 中的应用的情况下进行更新。我的建议就像@Rizwanatta 所说的那样。当您从服务器返回 JSON 时,请提供图标的 url。将路径存储在列表视图中。加载图像时,使用滑行(您无需担心缓存,它会为后续调用处理它)。如果您的过程花费太多时间,您可以在加载图标之前显示微调器。
  • 感谢各位的建议。我将研究 glide 以及它是如何完成的。

标签: android android-layout listview android-imageview


【解决方案1】:

正如 Rizwan 在 cmets 中所说,您可能希望使用 URL 从服务器加载图像。

要回答您问题的另一部分,您需要创建一个自定义 ListAdapter,SimpleAdapter 不会给您太大的灵活性。

下面我拿了一些我过去做的代码,并试图让它变得容易理解,如果你有任何问题,请告诉我?

class AdvancedAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<CustomData> mItems;

    AdvancedAdapter(Context context, ArrayList<CustomData> items) {
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        mItems = items;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.layout_for_list_item, null);
        }

        TextView titleView = (TextView) convertView.findViewById(R.id.title);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
        titleView.setText(mItems.get(position).name());
        imageView.setImageResource(0); //Nulls out the image while loading
......
...... Load image from URL
imageView.setImageBitmap((Bitmap) finalImage);
.....




        return convertView;
    }
}

【讨论】:

  • 谢谢亚当。由于我仍在学习 Java,所以我被困在不同的事情上,然后在我去的时候弄清楚它们。虽然表面上我理解你的代码,但尝试将其注入我的代码将是一个挑战。
  • 特别是在这种情况下,我试图弄清楚这个类定义的去向,以及它与填充 ListView 的关系。啊啊啊,我要开始玩了……
  • 您可以制作一个全新的 Java 文件,并用您制作的新适配器替换 SimpleAdapter 的创建。然后,您只需要填写特定于您的视图和数据的部分。如果您在任何特定部分遇到困难,请告诉我们!
  • 感谢您的建议。由于我仍然在这里学习,我正在努力理解这一切并在我去的时候弄清楚事情。你上面所说的对我来说很有意义,这是我弄清楚如何实际实现它的问题。如何从其他文件中调用类,我可以或不能引用某些东西......等等等等。我不擅长阅读书籍,我更擅长反复试验(有时会摸不着头脑。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多