我已经使用惰性列表概念实现了示例应用程序。我想将所有字符串数组值从LazyAdapter(扩展BaseAdapter)类分配给文本视图
我使用了这个类,如下所示
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、item1、item2、....etc一样。
发布于 2011-09-05 20:08:01
问题是因为您对每个要填充的项都使用了for循环。在本例中,由于for each view循环被执行,显然它是数组中的最后一个元素,因此在所有行中打印"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;
}}
这就是它所需要的。
发布于 2011-09-05 19:39:03
删除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;https://stackoverflow.com/questions/7307442
复制相似问题