2011-09-05 34 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

在您的代码中完全显示图像。 –