recyclerView中的setAdapter无法应用

时间:2019-06-30 18:08:36

标签: java android gridview android-recyclerview

因此,我遵循了一个在线教程,拥有一种图库应用程序。图片显示在GridView内部,但我需要将其更改为recyclerview。当我这样做时,虽然出现了该错误,但我的适配器似乎无法与recyclerview一起使用。这是我的适配器代码:

    class AlbumAdapter extends BaseAdapter {
        private Activity activity;
        private ArrayList<HashMap< String, String >> data;
        public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
            activity = a;
            data = d;
        }
        public int getCount() {
            return data.size();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            AlbumViewHolder holder = null;
            if (convertView == null) {
                holder = new AlbumViewHolder();
                convertView = LayoutInflater.from(activity).inflate(
                        R.layout.album_row, parent, false);

                holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
                holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
                holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);

                convertView.setTag(holder);
            } else {
                holder = (AlbumViewHolder) convertView.getTag();
            }
            holder.galleryImage.setId(position);
            holder.gallery_count.setId(position);
            holder.gallery_title.setId(position);

            HashMap < String, String > song = new HashMap < String, String > ();
            song = data.get(position);
            try {
                holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
                holder.gallery_count.setText(song.get(Function.KEY_COUNT));

                Glide.with(activity)
                        .load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
                        .into(holder.galleryImage);


            } catch (Exception e) {}
            return convertView;
        }
    }
    class AlbumViewHolder {
        ImageView galleryImage;
        TextView gallery_count, gallery_title;
    }

这是我设置适配器的活动中的一些代码:

@Override
    protected void onPostExecute(String xml) {

        AlbumAdapter adapter = new AlbumAdapter(ProfileActivity.this, albumList);
        galleryGridView.setAdapter(adapter);
        galleryGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    final int position, long id) {
                Intent intent = new Intent(ProfileActivity.this, AlbumActivity.class);
                intent.putExtra("name", albumList.get(+position).get(Function.KEY_ALBUM));
                startActivity(intent);
            }
        });
    }

这是我当前遇到的错误: Error message

我基本上想修复适配器,以便它在recyclerview中工作。

1 个答案:

答案 0 :(得分:0)

您必须创建RecyclerView.Adapter才能与RecyclerView一起使用。与BaseAdapter有点不同。让我给你一个如何实现这个例子。

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {

 @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.album_row, viewGroup, false);
        return new AlbumAdapter.ViewHolder(view);
    }

@Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        holder.galleryImage.setId(position);
        holder.gallery_count.setId(position);
        holder.gallery_title.setId(position);
    }
 @Override
    public int getItemCount() {
        return data.size();
    }

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView galleryImage;
    TextView gallery_count, gallery_title;
 public ViewHolder(View v) {
            galleryImage = v.findViewById(R.id.galleryImage);
            gallery_count =v.findViewById(R.id.gallery_count);
            gallery_title = v.findViewById(R.id.gallery_title);
    }
  }
}