下载管理器适配recyclerview

Download Manager to be adapted to recyclerview

我浏览了互联网上几乎所有关于从互联网下载文件的内容,其中一个教程有效
在那个教程中,他们使用了下载管理器,但它是一个按钮..i我正在使用带有单个下载按钮的项目的 recyclerview,并且项目列表的每个下载按钮都有不同的 url,请告诉我该怎么做!
我已经尝试过:(我知道这不是 personViewHolder.personbutton.setOnClickListener 的正确语法,但只是为了向您展示我想要的东西)

照片来自我的应用模拟器!

RVadapter.java

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    public static class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        Button personbutton;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
            personbutton=(Button)itemView.findViewById(R.id.imgbuttondownload);

        }
    }

    List<Person> persons;

    RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, final int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
        personViewHolder.personPhoto.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Context context=v.getContext();
                Intent intent=new Intent();
                switch (i){
                    case 0:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 1:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 2:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 3:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                }
            }
        });

        personViewHolder.personbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (i){
                    case 0:
                String url = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setDescription("my title");
                request.setTitle("my description");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager.enqueue(request);
                break;
                    case 1:
                        String url2 = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager2 = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request2 = new DownloadManager.Request(Uri.parse(url2));
                        request2.setDescription("my title");
                        request2.setTitle("my description");

                        request2.allowScanningByMediaScanner();
                        request2.setNotificationVisibility(
                                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                        request2.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager2.enqueue(request2);
                        break;


            }
        }
    });
    }

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

Mainactivity.java

public class Main4Activity extends Activity {

    private List<Person> persons;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main4);

        rv=(RecyclerView)findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);


        initializeData();
        initializeAdapter();

    }



    private void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("How to perform a staphylectomy | Surgical correction of RLN", "10:07", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perfom a clinical examination", "07:03", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a endoscopic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a staphylectomy | Surgical correction of RLN", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));

    }

    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(persons);
        rv.setAdapter(adapter);
    }

}

您只需将 URL 保存在 Person 对象中,然后每当发生点击时只需检索它并将其发送到下载管理器。 它将是这样的:

personViewHolder.personbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    Person person = persons.get(i);
                    String url = person.url;
                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                    request.setDescription(person.name);
                    request.setTitle(person.description);

                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+person.name+".mp4");
                    manager.enqueue(request);
        }});