在用于Recyclerview的适配器中创建循环Arraylist inisde ViewHolder

时间:2019-01-28 18:12:40

标签: java android android-recyclerview recycler-adapter

我正在忙于设置Festivals应用程序,该应用程序的区域中用户每周可以看到10首歌曲。每首歌曲都位于recyclerview中,其中的大多数按钮都允许用户通过我数据库中的URL发送到SoundCloud。我的recyclerview中有一个按钮,其作用类似于“收藏夹”按钮,一旦单击该按钮,就会将该歌曲保存为用户喜欢的数据库。我需要弄清楚如何在我的Recyclerview所使用的(ViewHolder)部分的适配器内部循环使用Arraylist,而不使用OnClick方法,以便当用户退出应用程序并重新进入应用程序时,自动从我的数据库中知道用户选择哪首歌曲作为他们的最爱。

我尝试在我的主要活动中创建一个方法,该方法会加载到onCreate中,该方法可让该应用查看用户在数据库中是否有喜欢的歌曲,如果有,该应用然后调用该方法以运行适配器Arraylist的值为“ Added”,“ Removed”或“ None”。根据此结果,应用程序随后会将我为适配器创建的行布局内的按钮设置为特定背景,而无需用户单击。我如何获得异常错误。

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

    private ArrayList<String> arrayListWeeklyTop10SinglesArtist;
    private ArrayList<String> arrayListWeeklyTop10SinglesName;
    private ArrayList<String> arrayListWeeklyTop10SinglesPic;
    private ArrayList<String> arrayListWeeklyTop10SinglesURL;
    private ArrayList<String> arrayListWeeklyTop10SinglesAddedRemovedNone;

    private Context mContext;

    String sWeeklyTopSingleURL, sWeeklyTopSingleName, sWeeklyTopSinglePic, sWeeklyTopSingleArtist;

    public AdapterRecycleWeeklyTop10Singles(ArrayList<String> arrayListWeeklyTop10SinglesArtist,
                                            ArrayList<String> arrayListWeeklyTop10SinglesName,
                                            ArrayList<String> arrayListWeeklyTop10SinglesPic,
                                            ArrayList<String> arrayListWeeklyTop10SinglesURL,
                                            ArrayList<String> arrayListWeeklyTop10SinglesAddedRemovedNone,
                                            Context mContext) {
        this.arrayListWeeklyTop10SinglesArtist = arrayListWeeklyTop10SinglesArtist;
        this.arrayListWeeklyTop10SinglesName = arrayListWeeklyTop10SinglesName;
        this.arrayListWeeklyTop10SinglesPic = arrayListWeeklyTop10SinglesPic;
        this.arrayListWeeklyTop10SinglesURL = arrayListWeeklyTop10SinglesURL;
        this.arrayListWeeklyTop10SinglesAddedRemovedNone = arrayListWeeklyTop10SinglesAddedRemovedNone;

        this.mContext = mContext;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(mContext).asBitmap().load(arrayListWeeklyTop10SinglesPic.get(position)).into(holder.image);
        holder.name.setText(arrayListWeeklyTop10SinglesName.get(position));
        holder.artist.setText(arrayListWeeklyTop10SinglesArtist.get(position));
    }

    @Override
    public int getItemCount() {
        return arrayListWeeklyTop10SinglesName.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView image;
        TextView name, artist;
        Button soundCloud, favWeeklySingle;

        int favSelected = 0;

        String sWeeklyAddedRemovedNone;


        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.imageViewUserWeeklyTopSongs);
            name = itemView.findViewById(R.id.textViewUserWeeklyTopSongsName);
            artist = itemView.findViewById(R.id.textViewUserWeeklyTopSongsArtist);
            soundCloud = itemView.findViewById(R.id.buttonWeeklyTopSC);
            favWeeklySingle = itemView.findViewById(R.id.buttonWeeklyTopFav);

            image.setOnClickListener(this);
            name.setOnClickListener(this);
            artist.setOnClickListener(this);
            soundCloud.setOnClickListener(this);
            favWeeklySingle.setOnClickListener(this);

            for (int i = 0; i < 10; i++) {
                sWeeklyAddedRemovedNone = arrayListWeeklyTop10SinglesAddedRemovedNone.get(i);

                if (sWeeklyAddedRemovedNone.equals("None") || sWeeklyAddedRemovedNone.equals("Removed")) {
                    favWeeklySingle.setBackgroundResource(R.drawable.ic_favorite_border_red);
                } else if (sWeeklyAddedRemovedNone.equals("Added")) {
                    favWeeklySingle.setBackgroundResource(R.drawable.ic_favorite_red);
                }
            }
        }

        public void soundCloudIntent() {
            Intent soundCloudFeaturedSongIntent = new Intent(Intent.ACTION_VIEW);
            String soundCloudFeaturedSongURL = sWeeklyTopSingleURL;
            soundCloudFeaturedSongIntent.setData(Uri.parse(soundCloudFeaturedSongURL));
            mContext.startActivity(soundCloudFeaturedSongIntent);
        }

        @Override
        public void onClick(View view) {
            sWeeklyTopSingleURL = arrayListWeeklyTop10SinglesURL.get(getLayoutPosition());
            switch (view.getId()) {
                case R.id.buttonWeeklyTopFav:
                    sWeeklyTopSingleName = arrayListWeeklyTop10SinglesName.get(getLayoutPosition());
                    sWeeklyTopSinglePic = arrayListWeeklyTop10SinglesPic.get(getLayoutPosition());
                    sWeeklyTopSingleArtist = arrayListWeeklyTop10SinglesArtist.get(getLayoutPosition());

                    Intent intentFavWeeklySingle = new Intent("user-fav-weekly-single-selected");

                /*if(favSelected == 0){
                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySingleAddRemove", "Added");
                    favWeeklySingle.setBackgroundResource(R.drawable.ic_favorite_red);
                }else{
                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySingleAddRemove", "Removed");
                    favWeeklySingle.setBackgroundResource(R.drawable.ic_favorite_border_red);
                    favSelected = -1;
                }
                favSelected++;*/

                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySingleURL", sWeeklyTopSingleURL);
                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySingleName", sWeeklyTopSingleName);
                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySinglePic", sWeeklyTopSinglePic);
                    intentFavWeeklySingle.putExtra("userSelectedFavWeeklySingleArtist", sWeeklyTopSingleArtist);
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(intentFavWeeklySingle);
                    break;
                case R.id.imageViewUserWeeklyTopSongs:
                    soundCloudIntent();
                    break;
                case R.id.textViewUserWeeklyTopSongsName:
                    soundCloudIntent();
                    break;
                case R.id.textViewUserWeeklyTopSongsArtist:
                    soundCloudIntent();
                    break;
                case R.id.buttonWeeklyTopSC:
                    soundCloudIntent();
                    break;
            }
        }
    }
}

0 个答案:

没有答案