Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.quiz;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.annotation.NonNull;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import com.example.quiz.AccountActivity.DownloadActivity;
- import com.example.quiz.AccountActivity.FilesActivity;
- import java.util.ArrayList;
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
- RecyclerView recyclerView;
- Context context;
- ArrayList<String> items = new ArrayList<>();
- ArrayList<String> urls = new ArrayList<>();
- public void update(String name, String url)
- {
- items.add(name);
- urls.add(url);
- notifyDataSetChanged(); // refreshes the recycler view automatically
- }
- public MyAdapter(RecyclerView recyclerView, Context context, ArrayList<String> items, ArrayList<String> urls) {
- this.recyclerView = recyclerView;
- this.context = context;
- this.items = items;
- this.urls = urls;
- }
- @NonNull
- @Override // to create views for recycler view item...
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
- return new ViewHolder(view);
- }
- @Override
- public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
- // initialise the elements of individual items...
- holder.nameOfFile.setText(items.get(position));
- }
- @Override // return the number of items...
- public int getItemCount() {
- return items.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder {
- TextView nameOfFile;
- // represents individual list items...
- public ViewHolder(View itemView) {
- super(itemView);
- nameOfFile = itemView.findViewById(R.id.nameOfFile);
- itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- int position=recyclerView.getChildLayoutPosition(view);
- Intent intent=new Intent();
- intent.setType(Intent.ACTION_VIEW); // denotes that we are going to view something
- intent.setData(Uri.parse(urls.get(position)));
- context.startActivity(intent); //launch the browser to display corresponding file
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement