Advertisement
pabloducato

MyAdapter.java

Jun 28th, 2019
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package com.example.quiz;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.support.annotation.NonNull;
  7. import android.support.v7.widget.RecyclerView;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.TextView;
  12.  
  13. import com.example.quiz.AccountActivity.DownloadActivity;
  14. import com.example.quiz.AccountActivity.FilesActivity;
  15.  
  16. import java.util.ArrayList;
  17.  
  18. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  19.  
  20.     RecyclerView recyclerView;
  21.     Context context;
  22.     ArrayList<String> items = new ArrayList<>();
  23.     ArrayList<String> urls = new ArrayList<>();
  24.  
  25.     public void update(String name, String url)
  26.     {
  27.         items.add(name);
  28.         urls.add(url);
  29.         notifyDataSetChanged(); // refreshes the recycler view automatically
  30.     }
  31.  
  32.  
  33.     public MyAdapter(RecyclerView recyclerView, Context context, ArrayList<String> items, ArrayList<String> urls) {
  34.         this.recyclerView = recyclerView;
  35.         this.context = context;
  36.         this.items = items;
  37.         this.urls = urls;
  38.     }
  39.  
  40.     @NonNull
  41.     @Override  // to create views for recycler view item...
  42.     public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  43.         View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
  44.         return new ViewHolder(view);
  45.     }
  46.  
  47.     @Override
  48.     public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  49.  
  50.         // initialise the elements of individual items...
  51.         holder.nameOfFile.setText(items.get(position));
  52.  
  53.  
  54.     }
  55.  
  56.     @Override  // return the number of items...
  57.     public int getItemCount() {
  58.         return items.size();
  59.     }
  60.  
  61.     public class ViewHolder extends RecyclerView.ViewHolder {
  62.  
  63.         TextView nameOfFile;
  64.  
  65.         // represents individual list items...
  66.         public ViewHolder(View itemView) {
  67.             super(itemView);
  68.             nameOfFile = itemView.findViewById(R.id.nameOfFile);
  69.             itemView.setOnClickListener(new View.OnClickListener() {
  70.                 @Override
  71.                 public void onClick(View view) {
  72.  
  73.                     int position=recyclerView.getChildLayoutPosition(view);
  74.                     Intent intent=new Intent();
  75.                     intent.setType(Intent.ACTION_VIEW); // denotes  that we are going to view something
  76.                     intent.setData(Uri.parse(urls.get(position)));
  77.                     context.startActivity(intent); //launch the browser to display corresponding file
  78.  
  79.                 }
  80.             });
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement