2016-02-28 50 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

相關問題