2014-01-09 28 views
0

我正在嘗試爲我的listView創建自定義適配器。這裏是我的CustomAdapter講座Android自定義適配器listView循環更多

public class CustomAdapter extends ArrayAdapter<Custom>{ 
private ArrayList<Custom> entries; 
private Activity activity; 
private int height; 

public CustomAdapter(int hei, Activity a, int textViewResourceId, ArrayList<Custom> entries) { 
    super(a, textViewResourceId, entries); 
    this.entries = entries; 
    this.activity = a; 
    this.height=hei; 
} 

public static class ViewHolder{ 
    public TextView item1; 
    public ImageView image; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    ViewHolder holder; 
    if (v == null) { 
     LayoutInflater vi = 
      (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.review_list_m, null); 
     v.setMinimumHeight(height); 
     holder = new ViewHolder(); 
     holder.item1 = (TextView) v.findViewById(R.id.name); 
     holder.image = (ImageView) v.findViewById(R.id.posterView); 
     v.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)v.getTag(); 

    final Custom custom = entries.get(position); 
    if (custom != null) { 
     String imgUrl=custom.getImage(); 
     Log.v("PATH",custom.getcustomBig()); 
     holder.item1.setText(custom.getcustomBig()); 

    } 
    return v; 
} 

} 

然後我customclass進入只有兩個值。
但以下行日誌中6次

Log.v("PATH",custom.getcustomBig()); 

喜歡的東西以下

01-09 11:49:34.683: V/PATH(12629): Name 1 
01-09 11:49:34.683: V/PATH(12629): Name 2 
01-09 11:49:34.683: V/PATH(12629): Name 1 
01-09 11:49:34.683: V/PATH(12629): Name 2 
01-09 11:49:34.683: V/PATH(12629): Name 1 
01-09 11:49:34.683: V/PATH(12629): Name 2 

但僅示出2 ListView中的條目。

有什麼奇怪的嗎?因爲我需要在listView上實現下載圖像腳本。在那種情況下,該圖像會被下載超過1次?

回答

0

試試這個代碼:(不要custom最後,不檢查null

Custom custom = entries.get(position); 
    String imgUrl=custom.getImage(); 
    Log.v("PATH",custom.getcustomBig()); 
    holder.item1.setText(custom.getcustomBig()); 
+0

嘗試過。但現在也顯示相同。我試過Log.v(「位置」,「 - 」+位置);然後0和1同時打印。 – ramesh

+0

listView中有多少個元素? –

0

能否請您檢查條目列出要傳遞到構造函數。將調用getView()來獲取條目列表中的條目數。您的條目列表可能包含6個值。

相關問題