2011-09-05 31 views
2

我已經通過使用懶惰列表concept.i想要分配的所有字符串數組值從LazyAdapter文本視圖實現示例應用程序(延伸BaseAdapter)類工作與LazyList問題

如下

我已經使用這個類
public class LazyAdapter extends BaseAdapter { 

private Activity activity; 
private String[] data; 
private static LayoutInflater inflater=null; 
public ImageLoader imageLoader; 



public LazyAdapter(Activity a, String[] d) { 
    activity = a; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader=new ImageLoader(activity.getApplicationContext()); 
} 

public int getCount() { 
    return data.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 


    if(convertView==null) 
     vi = inflater.inflate(R.layout.item, null); 



     String texts[]={"hai","how are you","hello","oyeeee"}; 

     //i would like to set this texts[] values to TextView 

     for(int i=0;i< texts.size();i++) 
     { 
     TextView text=(TextView)vi.findViewById(R.id.text);; 
     ImageView image=(ImageView)vi.findViewById(R.id.image); 
     text.setText("item "+texts[i]); 
     imageLoader.DisplayImage(data[position], activity, image); 


      } 
    return vi; 

     } 
    } 

在這裏,我能夠只顯示我oyeee.how可以查看所有值列表

這裏我怎麼可以在列表中顯示的所有字符串數組值就像懶列表,我們顯示item0,物品1, item2,....等,

回答

2

問題是因爲您已經爲每個正在填充的項目使用了for-loop。在這種情況下,因爲對於每個視圖,for循環都會執行,顯然它是數組中的最後一個元素,在所有行中都打印出「oyeeee」並不意外。

現在你要做的是什麼,你能提供的陣列全球某處您的活動,並在更改代碼getView()這樣,

//陣列的全局聲明

String texts[]={"hai","how are you","hello","oyeeee"}; 
public View getView(int position, View convertView, ViewGroup parent) { 
View vi=convertView; 


if(convertView==null) 
    vi = inflater.inflate(R.layout.item, null); 


    TextView text=(TextView)vi.findViewById(R.id.text); 
    ImageView image=(ImageView)vi.findViewById(R.id.image); 
    text.setText("item "+texts[postion]); 
    imageLoader.DisplayImage(data[position], activity, image); 


     } 
return vi; 

    } 

}

這就是所需要的。

1

刪除GetView代碼&在GetView函數中寫入以下代碼。

String texts[]={"hai","how are you","hello","oyeeee"}; 
View vi = convertView; 
     if (convertView == null) { 
      vi = inflater.inflate(R.layout.item, null); 
      image = (ImageView) vi.findViewById(R.id.imgview); 
      txtviewtitle=(TextView)vi.findViewById(R.id.txtviewtitle); 
     } 
     txtviewtitle.setText(texts[position]); 
     imageLoader.DisplayImage(data[position], activity, image); 

     return vi; 
+0

我只從字符串數組中獲取海字符串 –

+0

在您的代碼中完全顯示圖像。 –