简体   繁体   English

如何覆盖 ArrayAdapter add 方法以向其添加图像和文本视图? 我有自定义数组适配器类

[英]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.首先,我建议对StringInteger对象使用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 ,您保留对传递给适配器的StringInteger 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.

相关问题 如何在Java中覆盖类方法并向其添加“throws”声明? - How do I override a class method in Java and add a “throws” declaration to it? 如何将图像添加到 Textview [关闭] - How to add an image to the Textview [closed] ArrayAdapter添加方法中的ConcurrentModificationException - ConcurrentModificationException in ArrayAdapter add method 在适配器中在运行时添加TextView - Add TextView at runtime in Adapter 如何向此TextView添加自定义字体? - How to Add a custom Font to this TextView? customadapter与动态资源,我想将它们添加到arrayadapter中 <string> 这样我就可以将适配器添加到适配器中 - customadapter with dynamic resource and i want to add them in arrayadapter<string> so that i can add the,m into adapter 如何在 TextView 文本中添加图像? - How to add image in a TextView text? 如何将 onclick 添加到列表视图中的每个按钮,填充自定义数组适配器,从数组中删除该项目并更新它? - How to add an onclick to every button in a listview, populated with a custom arrayadapter, that deletes that item from the array and updates it? 如何在android中添加自定义数据到textview? - How can I add custom data to textview in android? 如何在scollview中的图像上方添加textview? - How can i add textview above an image in a scollview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM