Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们如何将Android下载管理器与RecyclerView结合使用?_Java_Android_Material Design_Android Recyclerview_Android Download Manager - Fatal编程技术网

Java 我们如何将Android下载管理器与RecyclerView结合使用?

Java 我们如何将Android下载管理器与RecyclerView结合使用?,java,android,material-design,android-recyclerview,android-download-manager,Java,Android,Material Design,Android Recyclerview,Android Download Manager,我有一个问题活动,其中我有一个多个问题。在RecyclerView的每个项目中。我有一个按钮,我想下载管理器开始下载时,这个按钮被点击 这是我的AdapterRecycler问题: public class AdapterRecyclerQuestion extends RecyclerView.Adapter<AdapterRecyclerQuestion.ViewHolder> { private Context context; private ArrayL

我有一个
问题活动
,其中我有一个多个问题。在
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();
    }
}
公共类AdapterRecyclerQuestion扩展了RecyclerView.Adapter{
私人语境;
私人ArrayList问题a;
//建造师
公共AdapterRecyclerQuestion(上下文,ArrayList questionha){
this.context=上下文;
this.questionha=questionha;
}
//观众
公共类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
私有文本视图txtTitle;
私有文本视图txtDesc;
私有文本视图txtCntDown;
私有ImageView imgAuthorPic;
私有文本视图txtAuthorName;
私有文本视图txtDate;
私人按钮BTN下载;
公共视图持有者(视图项视图){
超级(项目视图);
setOnClickListener(这个);
txtTitle=(TextView)itemView.findViewById(R.id.txt\u title\u question);
txtDesc=(TextView)itemView.findViewById(R.id.txt_desc_question);
txtCntDown=(TextView)itemView.findViewById(R.id.txt\u cnt\u down\u question\u dy);
imgAuthorPic=(ImageView)itemView.findViewById(R.id.img\u author\u pic\u question);
txtAuthorName=(TextView)itemView.findViewById(R.id.txt\u author\u name\u question);
txtDate=(TextView)itemView.findViewById(R.id.txt\u date\u question);
btnDownload=(按钮)itemviewbyd(R.id.btn\u down\u问题);
}
@凌驾
公共void onClick(视图v){
Toast.makeText(v.getContext(),“Hello”+txtTitle.getText(),Toast.LENGTH\u SHORT.show();
}
}
//oncreateviewholder
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.question_行,parent,false);
返回新的ViewHolder(视图);
}
//onbindviewholder
@凌驾
公共无效onBindViewHolder(最终视图持有人,最终整型位置){
holder.txtTitle.setText(questionha.get(position.getQuestionTitle());
holder.txtDesc.setText(questionha.get(position.getQuestionDesc());
holder.txtCntDown.setText(questionha.get(position.getQuestionDownCnt());
holder.txtouthorname.setText(questionha.get(position.getQuestionAuthorName());
Glide.with(holder.itemView.getContext()).load(questionha.get(position.getQuestionAuthorphic())到(holder.imgAuthorPic)中;
holder.txtDate.setText(questionha.get(position.getQuestionDate());
holder.btnDownload.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
=>DownloadManager DownloadManager=context.getSystemService(context.DOWNLOAD\u服务);
DownloadManager.Request Request=new DownloadManager.Request(Uri.parse(questionha.get(position.getQuestionDownLink());
request.setTitle(questionha.get(position.getQuestionTitle())
.setDescription(“下载”)
.setdestinationnexternalfilesdir(上下文,“问题”,questionha.get(position.getQuestionTitle());
}
});
}
//getitemcount
@凌驾
public int getItemCount(){
返回问题ha.size();
}
}

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

上下文。getSystemService
将返回一个
对象。由程序员将其强制转换为所需的类。因此,请将您的线路改为:

DownloadManager downloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
它将是
DownloadManager
类型,而不仅仅是
Object