[英]How to override ArrayAdapter add method to add image and textview to it? I have custom array adapter class
I am successfully adding image with text when I initialize adapter object and set it as listview adapter but I want to add more items to it when I get more data from internet which includes image.当我初始化适配器对象并将其设置为列表视图适配器时,我成功地添加了带有文本的图像,但是当我从包含图像的 Internet 获取更多数据时,我想向其中添加更多项目。 I am retrieving information from internet using json which includes image url.我正在使用包含图像 url 的 json 从互联网检索信息。 Here is my adapter code这是我的适配器代码
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
I want to add more items to it after I have set the array adapter.(images with text) I know how to add text only.我想在设置阵列适配器后向其中添加更多项目。(带有文本的图像)我只知道如何添加文本。
First of all, I would suggest to use an ArrayList
for the String
and Integer
objects, instead of normal arrays.首先,我建议对String
和Integer
对象使用ArrayList
,而不是普通数组。 With this you can more easily append data.有了这个,您可以更轻松地附加数据。
In your Activity
, you keep the reference to the String
and Integer
ArrayList
objects you pass to the Adapter.在您的Activity
,您保留对传递给适配器的String
和Integer
ArrayList
对象的引用。 When you receive new date, you simply add this data to the lists.当您收到新日期时,您只需将此数据添加到列表中即可。
When the data is updated, you should call the notifyDataSetChanged()
method of the adapter to display the data in your view.当数据更新时,您应该调用适配器的notifyDataSetChanged()
方法以在您的视图中显示数据。
--EDIT-- - 编辑 -
Another suggestion: you should recycle the views already created in the getView
method.另一个建议:你应该回收已经在getView
方法中创建的视图。 You can do this by checking if the provided contentview (the second argument) is null.您可以通过检查提供的内容视图(第二个参数)是否为空来完成此操作。 This will greatly improve the performance of larger ListViews
这将大大提高较大的ListViews
的性能
public View getView(int position, View view, ViewGroup parent) {
if(view == null){
LayoutInflater inflater = context.getLayoutInflater();
view= inflater.inflate(R.layout.list_single, null, true);
}
TextView txtTitle = (TextView) view.findViewById(R.id.txt);
ImageView imageView = (ImageView) view.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return view;
}
Try this at the beginning of your getView
method:在getView
方法的开头试试这个:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView= inflater.inflate(R.layout.list_single, null); //Try both with ", true" and without.
Also, try to build and pass a Drawable object to imageView.setImageResource(....);
另外,尝试构建一个 Drawable 对象并将其传递给 imageView.setImageResource imageView.setImageResource(....);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.