2

Ich habe eine QuestionActivity, wo ich eine multiple Frage darin habe. In jedem Artikel RecyclerView. Ich habe einen Button und ich möchte Download Manager den Download starten, wenn dieser Button angeklickt wird.Wie können wir Android Download Manager mit RecyclerView verwenden?

Dies ist mein 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(); 
    } 
} 

Nun, wenn ich meinen Code-Knopf hinzufügen onClick in onBindViewHolder es sagt, dass ich benötige nicht Java.lang.Object.

Antwort

1

context.getSystemService wird eine Object zurückgeben. Es liegt am Programmierer, es in die erforderliche Klasse zu übertragen. So Ihre Linie ändern:

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

Und es wird der Typ sein DownloadManager und nicht nur Object.