[英]getView() never called on custom adapter
I am aware that other people have asked this question, but I have looked at other solutions and still can't get it to work. 我知道其他人也问过这个问题,但是我看过其他解决方案,但仍然无法使它起作用。
Adapter code: 适配器代码:
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;
}
}
List item layout (itemlistrow.xml): 列表项布局(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>
In some of the other solutions, it mentions overriding getCount(). 在其他一些解决方案中,它提到了覆盖getCount()。 Is this what I am doing wrong? 这是我做错了吗? If so, how would I know what to put in for getCount(), as there are multiple different ArrayLists used. 如果是这样,我将如何知道要为getCount()输入什么,因为使用了多个不同的ArrayList。 Is it a case of picking one of them, as they are all the same length, eg timeArrayList.size()? 是否选择其中一个,因为它们的长度都相同,例如timeArrayList.size()?
Using multiple ArrayList
objects like that kind of defeats the purpose of using an ArrayAdapter
, whose idea is to have a single source of items. 像这样使用多个ArrayList
对象会破坏使用ArrayAdapter
的目的,后者的想法是只有一个项目源。 Not to mention that the code right now doesn't look nice at all. 更不用说现在的代码看起来一点也不好。
I'd suggest to first create a Weather
object that will hold your data: 我建议首先创建一个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....
.....
}
Than your adapter
can be transformed to something simpler like this: 比您的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;
}
}
Main difference is that it's now an ArrayAdapter<Weather>
and that you're passing the arguments directly in the constructor
of the adapter. 主要区别在于它现在是ArrayAdapter<Weather>
,并且您直接在适配器的constructor
中传递了参数。 Users of the adapter now have to call just 1 constructor, instead of all the final methods that had to be called before. 适配器的用户现在只需要调用1个构造函数,而不是之前必须调用的所有最终方法。
The other major difference is that you're passing the items
list to the super class. 另一个主要区别是您将items
列表传递给超类。 Now your adapter
knows it's size (internally getCount()
will be == items.size()
) so getView()
will be called appropriately. 现在您的adapter
知道了它的大小(内部getCount()
将为== items.size()
),因此将适当地调用getView()
。
As a final thought - the adapter
is still not using the ViewHolder
pattern, which you should totally implement! 最终ViewHolder
- adapter
仍未使用ViewHolder
模式,您应该完全实现它! There's been numerous posts for it, so just search a bit and you'll find it. 有关它的帖子很多,所以只要稍作搜索,便会找到它。
This is not a good way to populate a ListView
using an adapter which populates the data from multiple ArrayList
. 这不是使用适配器填充ListView
的好方法,该适配器填充来自多个ArrayList
的数据。 Generally we use a single source of dataset to be passed to an adapter in case of showing a list in Android. 通常,在Android中显示列表的情况下,我们使用单个数据集源传递给适配器。
So in your case, when you'll call the notifyDatasetChanged
it shouldn't take effect in the list properly as far as I can guess. 因此,就您而言,当您调用notifyDatasetChanged
时,据我notifyDatasetChanged
它不应在列表中正确生效。
notifyDatasetChanged
basically calls the getCount
function of the adapter and checks if the size of the ArrayList
associated with the adapter is changed or not. notifyDatasetChanged
基本上会调用适配器的getCount
函数,并检查与适配器关联的ArrayList
的大小是否已更改。 If the size of the ArrayList
is changed, it refreshes the ListView
and the getView
function gets called. 如果ArrayList
的大小更改,它将刷新ListView
并调用getView
函数。
In your case, I don't see any getCount
function though. 在您的情况下,我看不到任何getCount
函数。 getCount
usually returns the size of the ArrayList
associated with the adapter. getCount
通常返回与适配器关联的ArrayList
的大小。
So I would suggest, using a single ArrayList
to be passed to the adapter. 因此,我建议使用单个ArrayList
传递给适配器。 You can merge multiple ArrayList
and can use one joined HashMap
in your case too. 您可以合并多个ArrayList
,也可以在您的情况下使用一个连接的HashMap
。 Its your decision, exactly how you can pass a single list of your dataset to the adapter to populate them into a ListView
. 由您决定,以及如何将数据集的单个列表传递给适配器以将其填充到ListView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.