Java 使用ImageView填充阵列适配器

Java 使用ImageView填充阵列适配器,java,xml,android-studio,Java,Xml,Android Studio,请确保检查编辑,这很重要。 我正在学习Udacity课程“开发Android应用程序”,他们要求我们制作一个应用程序,使用MovieDB API加载流行电影的图像,并在网格视图中显示它们(以及其他内容,但这与现在无关) 我还应该用毕加索来加载图像。() 所以我所做的是: 创建了带有片段的空白活动 创建了一个名为grid\u item\u movie.xml的新xml文件 创建了由ImageView而不是字符串组成的适配器 使用异步任务将映像加载到适配器中 这是我的grid\u item\u mo

请确保检查编辑,这很重要。

我正在学习Udacity课程“开发Android应用程序”,他们要求我们制作一个应用程序,使用MovieDB API加载流行电影的图像,并在网格视图中显示它们(以及其他内容,但这与现在无关)

我还应该用毕加索来加载图像。()

所以我所做的是:

  • 创建了带有片段的空白活动
  • 创建了一个名为grid\u item\u movie.xml的新xml文件
  • 创建了由ImageView而不是字符串组成的适配器
  • 使用异步任务将映像加载到适配器中
  • 这是我的grid\u item\u movie.xml的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:id="@+id/grid_item_movie_imageview">
    
    </TextView>
    
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivityFragment">
    
        <GridView
            android:id="@+id/gridview_movies"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </GridView>
    
    </FrameLayout>
    
    我这样声明ImageAdapter:

    public class ImageAdapter extends ArrayAdapter
    {
        private final Context context;
        private ArrayList<String> imagesURL;
        public ImageAdapter(Context context, int resource, ArrayList<String> objects)
        {
            super(context, resource, objects);
            this.context = context;
            this.imagesURL = objects;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View myGridView = inflater.inflate(R.layout.grid_item_movie, parent, false);
            ImageView moviePoster = (ImageView) myGridView.findViewById(R.id.grid_item_movie_imageview);
            Picasso.with(context).load(imagesURL.get(position)).into(moviePoster);
            return myGridView;
        }
    }
    
    mMovieAdapter = new ImageAdapter(
            getContext(),
            R.layout.grid_item_movie,
            new ArrayList<String>());
    
    mMovieAdapter=新图像适配器(
    getContext(),
    R.layout.grid\u项目\u电影,
    新的ArrayList());
    
    此外,文件网格项目电影现在有一个ImageView作为根元素。
    应用程序没有崩溃,但没有显示任何内容,这是为什么?

    列表视图中的每个项目都会调用一次getView方法。但仅当项目可见时。因此,在实现此方法时,性能是至关重要的,当用户快速滚动时,滚动将是不稳定的。它涉及重用视图并保留这些视图的引用以帮助提高性能。一个优秀的教程可以在这里找到。它非常深入,涉及基本和高级主题。下面是一个简单的sudocode实现:

    private static class ViewHolder {
        // holds onto references to views needed in the list view
     }
    
    getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // inflate the item view this will be reused as the user scrolls
            // perform tasks that are the same across all items
        }
    
        // perform tasks that are different for each item
    
        return convertView;
    }
    

    当你试图显示图像时,为什么grid_item_movie.xml包含文本视图?@RobRose,正如我所说的-最初我使用的是ImageView而不是TextView,但我的应用程序崩溃了,它说我必须向Adpeter提供文本视图的id。虽然我不知道是什么原因,我可以告诉你文本出现的原因是因为你试图将图像加载到文本视图中。“它在做你叫它做的事。”罗布罗斯,我知道。问题是,如何将其转换为ImageView?谢谢,但您需要了解我是初学者。我需要知道如何将文本视图
    转换为图像视图,这一切都是通过毕加索服务完成的。你不能将文本视图转换为图像视图,但所有视图都有一个方法setBackgroundResource(int resourceId),你可以调用该方法将背景设置为你拥有的资源。例如,如果在drawable文件夹中有一个名为item_background.png的背景图像,则可以调用anyView.setBackgroundResource(R.drawable.item_background);你说得对,但正如我所说,我使用毕加索服务()从互联网上获取图像。未从drawable文件夹接收图像,因此我无法使用setBackgroundResource()或setImageResource()。此答案可能有助于说明如何将图像设置为目标。在onBitmapLoaded方法中,第一行将位图转换为可绘制的位图。然后,您可以忽略其余部分,只需调用aTextView.setBackground(可撤销);我认为这应该行得通。
    
    mMovieAdapter = new ImageAdapter(
            getContext(),
            R.layout.grid_item_movie,
            new ArrayList<String>());
    
    private static class ViewHolder {
        // holds onto references to views needed in the list view
     }
    
    getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // inflate the item view this will be reused as the user scrolls
            // perform tasks that are the same across all items
        }
    
        // perform tasks that are different for each item
    
        return convertView;
    }