2016-02-28 49 views
0

我知道其他人已经问过这个问题,但我已经看过其他解决方案,但仍然无法使其工作。getView()从来没有调用自定义适配器

适配器代码:

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: 

    <?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()?

+0

[ArrayAdapter的getView()方法没有被调用]的可能的重复](http://stackoverflow.com/questions/9730328/the-getview-method-of-arrayadapter-is-not-getting-called) –

+0

而不是getter和setter,您可以将列表传递给适配器构造函数。在超级调用'super(context,resource,yourlist)'中。注意;如果你更新你的底层数据,填充列表不要忘记在你的适配器上调用'notifyDataSetChanged()'。 – Raghunandan

+0

@Michael Nares不要在构造函数的super中传递resourceid。多数民众赞成你getView不叫。请参阅http://coderzpassion.com/android-listview-with-image-and-text/ –

回答

2

使用多个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个构造函数,而不是之前必须调用的所有最终方法。

另一个主要区别是,您将items列表传递给超类。现在你的adapter知道它的大小(内部getCount()将是== items.size()),所以getView()将被适当调用。

作为最后的想法 - adapter仍然没有使用ViewHolder模式,你应该完全实现!它有很多帖子,所以只需搜索一下,你就能找到它。

+0

我不需要重写getItem(),对吧? –

+0

不,你不需要。它会从你在构造函数中传递的列表中返回适当的'Weather'对象。最后 - 我真的强调实施'ViewHolder'模式的重要性:) – Vesko

1

这不是一个使用适配器填充ListView的好方法,该适配器填充来自多个ArrayList的数据。通常,我们使用单个数据源来传递给适配器,以便在Android中显示列表。

所以在你的情况下,当你打电话给notifyDatasetChanged它不应该适当地在列表中生效,据我所知可以。

notifyDatasetChanged基本上调用适配器的getCount函数,并检查与适配器关联的ArrayList的大小是否发生变化。如果ArrayList的大小发生更改,则会刷新ListView并调用getView函数。

在你的情况,虽然我没有看到任何getCount函数。 getCount通常会返回与适配器关联的ArrayList的大小。

所以我会建议,使用一个单一的ArrayList传递给适配器。您可以合并多个ArrayList,也可以在您的案例中使用一个加入的HashMap。它是您的决定,您可以将数据集的单个列表传递给适配器,以便将它们填充到ListView中。

+1

如果您在超级调用中提供了适当的参数,则会在内部调用getCount()。可以通过查看ArrayAdapter代码来检查。你可以用setter和getter来代替所有这些数组列表。 – Raghunandan

相关问题