【问题标题】:How we can Use Android Download Manager with RecyclerView?我们如何将 Android 下载管理器与 RecyclerView 一起使用?
【发布时间】:2016-09-12 08:43:54
【问题描述】:

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

这是我的 AdapterRecyclerQuestion:

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

【问题讨论】:

    标签: java android material-design android-recyclerview android-download-manager


    【解决方案1】:

    context.getSystemService 将返回一个Object。由程序员将其转换为所需的类。所以将你的行改为:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 2016-10-21
      • 2019-05-30
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多