Search code examples
androidandroid-recyclerviewandroid-download-manager

Download Manager to be adapted to recyclerview


I went through almost all what is available on internet about downloading a file from internet and one of the tutorials worked
In that tutorial they've used download manager but it was for a button ..i'm using a recyclerview with items having a single dwonload button and each download button of the items list has a different url , please show me how to do that!
what i've tried:( i know it's not the right syntax for personViewHolder.personbutton.setOnClickListener but just to show you what i kinda want)

Photo from the emulator of my app!

pic

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);
    }

}

Solution

  • you can simply hold your URL in Person object and then whenever a click happens just retrieve it and send it to download manager. it will be something like this:

    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);
            }});