• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.example.android.scopeddirectoryaccess;
2 
3 import android.provider.DocumentsContract;
4 import android.support.v7.widget.RecyclerView;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.ImageView;
9 import android.widget.TextView;
10 
11 import java.util.ArrayList;
12 import java.util.List;
13 
14 /**
15  * Provide views to RecyclerView with the directory entries.
16  */
17 public class DirectoryEntryAdapter extends RecyclerView.Adapter<DirectoryEntryAdapter.ViewHolder> {
18 
19     private List<DirectoryEntry> mDirectoryEntries;
20 
DirectoryEntryAdapter()21     public DirectoryEntryAdapter() {
22         this(new ArrayList<DirectoryEntry>());
23     }
24 
DirectoryEntryAdapter(List<DirectoryEntry> directoryEntries)25     public DirectoryEntryAdapter(List<DirectoryEntry> directoryEntries) {
26         mDirectoryEntries = directoryEntries;
27     }
28 
29     @Override
onCreateViewHolder(ViewGroup viewGroup, int viewType)30     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
31         View v = LayoutInflater.from(viewGroup.getContext())
32                 .inflate(R.layout.directory_entry, viewGroup, false);
33         return new ViewHolder(v);
34     }
35 
36     @Override
onBindViewHolder(ViewHolder viewHolder, final int position)37     public void onBindViewHolder(ViewHolder viewHolder, final int position) {
38         viewHolder.fileName.setText(mDirectoryEntries.get(position).fileName);
39         viewHolder.mimeType.setText(mDirectoryEntries.get(position).mimeType);
40 
41         if (DocumentsContract.Document.MIME_TYPE_DIR
42                 .equals(mDirectoryEntries.get(position).mimeType)) {
43             viewHolder.imageView.setImageResource(R.drawable.ic_directory_grey600_36dp);
44         } else {
45             viewHolder.imageView.setImageResource(R.drawable.ic_description_grey600_36dp);
46         }
47     }
48 
49     @Override
getItemCount()50     public int getItemCount() {
51         return mDirectoryEntries.size();
52     }
53 
setDirectoryEntries(List<DirectoryEntry> directoryEntries)54     public void setDirectoryEntries(List<DirectoryEntry> directoryEntries) {
55         mDirectoryEntries = directoryEntries;
56     }
57 
58     /**
59      * Provide a reference to the type of views that you are using (custom ViewHolder)
60      */
61     public class ViewHolder extends RecyclerView.ViewHolder {
62 
63         public TextView fileName;
64         public TextView mimeType;
65         public ImageView imageView;
66 
ViewHolder(View v)67         public ViewHolder(View v) {
68             super(v);
69             fileName = (TextView) v.findViewById(R.id.textview_filename);
70             mimeType = (TextView) v.findViewById(R.id.textview_mimetype);
71             imageView = (ImageView) v.findViewById(R.id.imageview_entry);
72         }
73     }
74 }
75 
76