java - getView() 从未在自定义适配器上调用过

标签 java android android-arrayadapter

我知道其他人也问过这个问题,但我看过其他解决方案,但仍然无法使其发挥作用。

适配器代码:

private class CustomTextAndImageAdapter extends ArrayAdapter<String> {
    private Context context;
    private Activity activity;
    private ArrayList<String> timeArrayList;
    private ArrayList<Bitmap> weatherIconArrayList;
    private ArrayList<String> descriptionArrayList;
    private ArrayList<String> tempArrayList;
    private ArrayList<String> popArrayList;
    private ArrayList<String> windSpeedArrayList;

    public final void setTimeArrayList(ArrayList<String> timeArrayList)
    {
        this.timeArrayList = timeArrayList;
    }

    public final void setDescriptionArrayList(ArrayList<String> descriptionArrayList)
    {
        this.descriptionArrayList = descriptionArrayList;
    }

    public final void setTempArrayList(ArrayList<String> tempArrayList)
    {
        this.tempArrayList = tempArrayList;
    }

    public final void setPopArrayList(ArrayList<String> popArrayList)
    {
        this.popArrayList = popArrayList;
    }

    public final void setWindSpeedArrayList(ArrayList<String> windSpeedArrayList)
    {
        this.windSpeedArrayList = windSpeedArrayList;
    }

    public final void setWeatherIconArrayList(ArrayList<Bitmap> weatherIconArrayList)
    {
        this.weatherIconArrayList = weatherIconArrayList;
    }

    public CustomTextAndImageAdapter(Context context, Activity activity, int resource)
    {
        super(context, resource);
        this.context = context;
        this.activity = activity;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        Log.d(Constants.LOG_TAG, "getView() method called");
        LayoutInflater inflater = activity.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.itemlistrow, null, false);

        TextView timeTextView = (TextView)rowView.findViewById(R.id.time);
        timeTextView.setText(timeArrayList.get(position));
        Log.d(Constants.LOG_TAG, "Time text view text = " + timeArrayList.get(position));

        ImageView iconImageView = (ImageView) rowView.findViewById(R.id.weatherIcon);
        iconImageView.setImageBitmap(weatherIconArrayList.get(position));

        TextView descriptionTextView = (TextView)rowView.findViewById(R.id.description);
        descriptionTextView.setText(descriptionArrayList.get(position));

        TextView tempTextView = (TextView)rowView.findViewById(R.id.temp);
        tempTextView.setText(tempArrayList.get(position));

        TextView popTextView = (TextView)rowView.findViewById(R.id.pop);
        popTextView.setText(popArrayList.get(position));

        TextView windSpeedTextView = (TextView)rowView.findViewById(R.id.windSpeed);
        windSpeedTextView.setText(windSpeedArrayList.get(position));

        return rowView;
    }
}

列表项布局(itemlistrow.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/time" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/weatherIcon"
                />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sunny"
                android:id="@+id/description"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="11 C"
                android:id="@+id/temp"
                />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "Rain:"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "Wind:"
                />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id = "@+id/pop"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "@+id/windSpeed"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在其他一些解决方案中,它提到了重写 getCount()。这是我做错了吗?如果是这样,我怎么知道要为 getCount() 输入什么,因为使用了多个不同的 ArrayList。是否是选择其中一个的情况,因为它们的长度都相同,例如timeArrayList.size()?

最佳答案

使用多个 ArrayList像这样的对象违背了使用 ArrayAdapter 的目的,其想法是拥有单一的元素来源。更不用说现在的代码看起来一点也不好看。

我建议首先创建一个 Weather将保存您的数据的对象:

public class Weather {
    private String time;
    private Bitmap weatherIcon;
    private String description;
    private String temp;
    private String pop;
    private String windSpeed;

    // build object here, provide getters, etc....
    .....
}

比你的adapter可以转换为更简单的东西,如下所示:

private class CustomTextAndImageAdapter extends ArrayAdapter<Weather>
{
    private LayoutInflater inflater;

    public CustomTextAndImageAdapter(Context context, Activity activity, int resource, List<Weather> items)
    {
        super(context, resource, items);
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        View rowView= inflater.inflate(R.layout.itemlistrow, null, false);

        TextView timeTextView = (TextView)rowView.findViewById(R.id.time);
        timeTextView.setText(getItem(position).getTime());

        ImageView iconImageView = (ImageView) rowView.findViewById(R.id.weatherIcon);
        iconImageView.setImageBitmap(getItem(position).getWeatherIcon());

        ........

        return rowView;
    }
}

主要区别在于它现在是 ArrayAdapter<Weather>并且您直接在 constructor 中传递参数适配器的。适配器的用户现在只需调用 1 个构造函数,而不是之前必须调用的所有 final方法。

另一个主要区别是您传递了 items列出到 super 类。现在你的adapter知道它的大小(内部 getCount() 将 == items.size() )所以 getView()将被适本地调用。

最后一个想法 - adapter仍然没有使用ViewHolder模式,你应该完全实现!关于它的帖子有很多,所以只要稍微搜索一下就可以找到。

关于java - getView() 从未在自定义适配器上调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680460/

相关文章:

滚动gridview时重复调用Gridview的第一个位置

javascript - Jframe 等同于 javascript

JavaFX Web View : custom cursors not working?

Android 蓝牙设备仅扫描一次

java - Android 将 2 个 Mp4 字节数组附加在一起,然后输出 1 个 Mp4 文件(其中最终视频是第一个视频加上第二个视频)

android - 适用于移动设备的 Adob​​e Air 中的表格组件

java - 每次我获取 pdf 文件时,我的 ArrayAdapter 堆栈

Android - 自定义 ListView 适配器 - 多选删除 - Indexoutofbounds - 为什么?

java - ThreadPoolExecutor参数配置

java - 提高油漆质量