我们如何将 Android 下载管理器与 RecyclerView 一起使用?

How we can Use Android Download Manager with RecyclerView?

我有一个 QuestionActivity 其中有多个问题。在 RecyclerView 的每个项目中。我有一个按钮,我希望下载管理器在单击此按钮时开始下载。

这是我的 AdapterRecycler 问题:

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


    private Context context;
    private ArrayList<ModelQuestion> questionha;

    //constructor

    public AdapterRecyclerQuestion(Context context, ArrayList<ModelQuestion> questionha) {
        this.context = context;
        this.questionha = questionha;
    }

    //viewholder
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView txtTitle;
        private TextView txtDesc;
        private TextView txtCntDown;
        private ImageView imgAuthorPic;
        private TextView txtAuthorName;
        private TextView txtDate;
        private Button btnDownload;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            txtTitle = (TextView) itemView.findViewById(R.id.txt_title_question);
            txtDesc = (TextView) itemView.findViewById(R.id.txt_desc_question);
            txtCntDown = (TextView) itemView.findViewById(R.id.txt_cnt_down_question_dy);
            imgAuthorPic = (ImageView) itemView.findViewById(R.id.img_author_pic_question);
            txtAuthorName = (TextView) itemView.findViewById(R.id.txt_author_name_question);
            txtDate = (TextView) itemView.findViewById(R.id.txt_date_question);
            btnDownload = (Button) itemView.findViewById(R.id.btn_down_question);

        }

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Hello" + txtTitle.getText(), Toast.LENGTH_SHORT).show();
        }
    }
    //oncreateviewholder

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

    //onbindviewholder

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
        holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
        holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
        holder.txtAuthorName.setText(questionha.get(position).getQuestionAuthorName());
        Glide.with(holder.itemView.getContext()).load(questionha.get(position).getQuestionAuthorPic()).into(holder.imgAuthorPic);
        holder.txtDate.setText(questionha.get(position).getQuestionDate());
        holder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 ====>               DownloadManager downloadManager = context.getSystemService(context.DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(questionha.get(position).getQuestionDownLink()));
                request.setTitle(questionha.get(position).getQuestionTitle())
                        .setDescription("Downloading")
                        .setDestinationInExternalFilesDir(context, "Questions",questionha.get(position).getQuestionTitle());

            }
        });

    }

    //getitemcount
    @Override
    public int getItemCount() {
        return questionha.size();
    }
}

现在,当我将我的代码添加到 onBindViewHolder 中的按钮 onClick 时,它说我需要 android.app.DownloadManager 而不是 Java.lang.Object

context.getSystemService 将 return 和 Object。由程序员将其转换为所需的 class。因此,将您的行更改为:

DownloadManager downloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);

而且它将是 DownloadManager 类型,而不仅仅是 Object