├── MusicLoaderExample ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── simpleware │ │ │ └── jonathan │ │ │ └── musicloaderexample │ │ │ └── ApplicationTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── simpleware │ │ │ │ └── jonathan │ │ │ │ └── musicloaderexample │ │ │ │ ├── LoaderActivity.java │ │ │ │ ├── Music.java │ │ │ │ ├── MusicAdapter.java │ │ │ │ └── MusicLoader.java │ │ └── res │ │ │ ├── drawable-hdpi │ │ │ └── ic_music_note_black_48dp.png │ │ │ ├── drawable-mdpi │ │ │ └── ic_music_note_black_48dp.png │ │ │ ├── drawable-xhdpi │ │ │ └── ic_music_note_black_48dp.png │ │ │ ├── drawable-xxhdpi │ │ │ └── ic_music_note_black_48dp.png │ │ │ ├── drawable-xxxhdpi │ │ │ └── ic_music_note_black_48dp.png │ │ │ ├── layout │ │ │ ├── activity_loader.xml │ │ │ └── list_item.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── simpleware │ │ └── jonathan │ │ └── musicloaderexample │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle └── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── simpleware │ └── jonathan │ └── listviewexample │ └── ApplicationTest.java └── main ├── AndroidManifest.xml ├── java └── com │ └── simpleware │ └── jonathan │ └── listviewexample │ └── MainActivity.java └── res ├── layout ├── activity_main.xml └── list_item.xml ├── menu └── menu_main.xml ├── mipmap-hdpi └── ic_launcher.png ├── mipmap-mdpi └── ic_launcher.png ├── mipmap-xhdpi └── ic_launcher.png ├── mipmap-xxhdpi └── ic_launcher.png ├── values-w820dp └── dimens.xml └── values ├── dimens.xml ├── strings.xml └── styles.xml /MusicLoaderExample/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.simpleware.jonathan.musicloaderexample" 9 | minSdkVersion 14 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.4.0' 26 | // This dependecy is req. to use the recylclerview. 27 | compile 'com.android.support:recyclerview-v7:23.1.+' 28 | } 29 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\tazfd\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/androidTest/java/com/simpleware/jonathan/musicloaderexample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/java/com/simpleware/jonathan/musicloaderexample/LoaderActivity.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | import android.content.Context; 4 | import android.content.CursorLoader; 5 | import android.database.Cursor; 6 | import android.support.v4.app.LoaderManager; 7 | import android.support.v4.content.Loader; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.os.Bundle; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.widget.CursorAdapter; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | public class LoaderActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks> { 18 | 19 | private RecyclerView mRecyclerView; 20 | private MusicAdapter musicAdapter; 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_loader); 25 | mRecyclerView = (RecyclerView) findViewById(R.id.recyclerVw); 26 | // Use a LinearLayoutManager to make the RecyclerView display the Music in a vertically scrolling list. 27 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 28 | musicAdapter = new MusicAdapter(this, new ArrayList()); 29 | mRecyclerView.setAdapter(musicAdapter); 30 | 31 | final LoaderManager supportLoaderManager = getSupportLoaderManager(); 32 | supportLoaderManager.initLoader(1, null, this); 33 | } 34 | 35 | @Override 36 | public Loader> onCreateLoader(int id, Bundle args) { 37 | return new MusicLoader(this); 38 | } 39 | 40 | 41 | @Override 42 | public void onLoadFinished(Loader> loader, List data) { 43 | // Add the newly loaded music to adapter. 44 | musicAdapter.addItems(data); 45 | } 46 | 47 | @Override 48 | public void onLoaderReset(Loader> loader) { 49 | // Clear the old music because a new list is going to be coming. 50 | musicAdapter.clearItem(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/java/com/simpleware/jonathan/musicloaderexample/Music.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | /** 4 | * Created by JDavis on 7/26/2016. 5 | */ 6 | public class Music { 7 | private long id; 8 | private long albumId; 9 | private String title; 10 | private String artist; 11 | 12 | public long getId() { 13 | return id; 14 | } 15 | 16 | public void setId(long id) { 17 | this.id = id; 18 | } 19 | 20 | public long getAlbumId() { 21 | return albumId; 22 | } 23 | 24 | public void setAlbumId(long albumId) { 25 | this.albumId = albumId; 26 | } 27 | 28 | public String getTitle() { 29 | return title; 30 | } 31 | 32 | public void setTitle(String title) { 33 | this.title = title; 34 | } 35 | 36 | public String getArtist() { 37 | return artist; 38 | } 39 | 40 | public void setArtist(String artist) { 41 | this.artist = artist; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/java/com/simpleware/jonathan/musicloaderexample/MusicAdapter.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | import android.content.ContentUris; 4 | import android.content.Context; 5 | import android.content.res.Resources; 6 | import android.graphics.Bitmap; 7 | import android.graphics.drawable.BitmapDrawable; 8 | import android.graphics.drawable.Drawable; 9 | import android.net.Uri; 10 | import android.os.AsyncTask; 11 | import android.provider.MediaStore; 12 | import android.support.v4.util.LruCache; 13 | import android.support.v7.widget.RecyclerView; 14 | import android.view.LayoutInflater; 15 | import android.view.View; 16 | import android.view.ViewGroup; 17 | import android.widget.ImageView; 18 | import android.widget.ListView; 19 | import android.widget.TextView; 20 | 21 | import org.w3c.dom.Text; 22 | 23 | import java.io.FileNotFoundException; 24 | import java.io.IOException; 25 | import java.lang.ref.WeakReference; 26 | import java.net.URI; 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | /** 31 | * Created by JDavis on 7/26/2016. 32 | */ 33 | public class MusicAdapter extends RecyclerView.Adapter { 34 | 35 | Context mContext; 36 | List mMusic; 37 | BitmapDrawable mPlaceholder; 38 | LruCache mBitmapCache; 39 | 40 | public MusicAdapter(Context context, List music) { 41 | mMusic = new ArrayList<>(); 42 | if(music != null) { 43 | mMusic.addAll(music); 44 | } 45 | mContext = context; 46 | mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp); 47 | // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes. 48 | int maxSize = (int) (Runtime.getRuntime().maxMemory() / 1024); 49 | // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps. 50 | int cacheSize = maxSize / 8; 51 | mBitmapCache = new LruCache(cacheSize) { 52 | 53 | @Override 54 | protected int sizeOf(Long key, Bitmap value) { 55 | // returns the size of bitmaps in kilobytes. 56 | return value.getByteCount() / 1024; 57 | } 58 | }; 59 | } 60 | 61 | /** 62 | * Adds a {@link Music} item to the Adapter. 63 | * @param item 64 | */ 65 | public void addItem(Music item) { 66 | // Append the music item to the end of the list. 67 | mMusic.add(item); 68 | // Notify the Recyclerview that an item has been inserted at our tail. 69 | notifyItemInserted(mMusic.size() - 1); 70 | } 71 | 72 | /** 73 | * Adds a {@link List} of {@link Music} to the adapters. 74 | * This method replaces the current music items inside of the adapter with the specified music items. 75 | * @param music 76 | */ 77 | public void addItems(List music) { 78 | // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items. 79 | mMusic.clear(); 80 | // Add the new music list. 81 | mMusic.addAll(music); 82 | notifyItemRangeInserted(0, music.size()); 83 | } 84 | 85 | /** 86 | * Clears the {@link Music} items inside of this adapter. 87 | */ 88 | public void clearItem() { 89 | mMusic.clear(); 90 | } 91 | 92 | @Override 93 | public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 94 | LayoutInflater inflater = LayoutInflater.from(mContext); 95 | View v = inflater.inflate(R.layout.list_item, parent, false); 96 | MusicViewHolder musicViewHolder = new MusicViewHolder(v); 97 | return musicViewHolder; 98 | } 99 | 100 | @Override 101 | public void onBindViewHolder(MusicViewHolder holder, int position) { 102 | Music music = mMusic.get(position); 103 | // Check the Bitmap cache for the album art first.. 104 | final Bitmap bitmap = mBitmapCache.get(music.getAlbumId()); 105 | // If the bitmap is not null, then use the cached images. 106 | if(bitmap != null){ 107 | holder.icon.setImageBitmap(bitmap); 108 | } 109 | else { 110 | // No album art could be found in the cache try reloading it. 111 | // In a real work example you should check that this value is not some junk value indicating that their is no album artwork. 112 | loadAlbumArt(holder.icon, music.getAlbumId()); 113 | } 114 | holder.artist.setText(music.getArtist()); 115 | holder.title.setText(music.getTitle()); 116 | } 117 | 118 | /** 119 | * Helper method for asynchronously loading album art. 120 | * @param icon 121 | * @param albumId 122 | */ 123 | public void loadAlbumArt(ImageView icon, long albumId) { 124 | // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id. 125 | if(cancelLoadTask(icon, albumId)) { 126 | // There was either no task running or it was loading a different image so create a new one to load the proper image. 127 | LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext); 128 | // Store the task inside of the async drawable. 129 | AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt); 130 | icon.setImageDrawable(drawable); 131 | loadAlbumArt.execute(albumId); 132 | } 133 | } 134 | 135 | /** 136 | * Helper method cancelling {@link LoadAlbumArt}. 137 | * 138 | * @param icon 139 | * @param albumId 140 | * @return 141 | */ 142 | public boolean cancelLoadTask(ImageView icon, long albumId) { 143 | LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon); 144 | // If the task is null return true because we want to try and load the album art. 145 | if(loadAlbumArt == null) { 146 | return true; 147 | } 148 | if(loadAlbumArt != null) { 149 | // If the album id differs cancel this task because it cannot be recycled for this imageview. 150 | if(loadAlbumArt.albumId != albumId) { 151 | loadAlbumArt.cancel(true); 152 | return true; 153 | } 154 | } 155 | return false; 156 | } 157 | 158 | /** 159 | * Helper method for extracting an {@link LoadAlbumArt}. 160 | * @param icon 161 | * @return 162 | */ 163 | public AsyncTask getLoadTask(ImageView icon) { 164 | LoadAlbumArt task = null; 165 | Drawable drawable = icon.getDrawable(); 166 | if(drawable instanceof AsyncDrawable) { 167 | task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 168 | } 169 | return task; 170 | } 171 | 172 | private class LoadAlbumArt extends AsyncTask { 173 | 174 | // URI that points to the AlbumArt database. 175 | private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart"); 176 | public WeakReference mIcon; 177 | // Holds a publicly accessible albumId to be checked against. 178 | public long albumId; 179 | private Context mContext; 180 | int width, height; 181 | 182 | public LoadAlbumArt(ImageView icon, Context context) { 183 | // Store a weak reference to the imageView. 184 | mIcon = new WeakReference(icon); 185 | // Store the width and height of the imageview. 186 | // This is necessary for properly scalling the bitmap. 187 | width = icon.getWidth(); 188 | height = icon.getHeight(); 189 | mContext = context; 190 | } 191 | 192 | @Override 193 | protected void onPostExecute(Bitmap bitmap) { 194 | if(isCancelled() || bitmap == null){ 195 | return; 196 | } 197 | // Check to make sure that the imageview has not been garbage collected as well as the 198 | // LoadArtworkTask is the same as this one. 199 | if(mIcon != null && mIcon.get() != null) { 200 | ImageView icon = mIcon.get(); 201 | Drawable drawable = icon.getDrawable(); 202 | if(drawable instanceof AsyncDrawable) { 203 | LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask(); 204 | // Make sure that this is the same task as the one current stored inside of the ImageView's drawable. 205 | if(task != null && task == this) { 206 | icon.setImageBitmap(bitmap); 207 | } 208 | } 209 | } 210 | mBitmapCache.put(albumId, bitmap); 211 | super.onPostExecute(bitmap); 212 | } 213 | 214 | @Override 215 | protected Bitmap doInBackground(Long... params) { 216 | // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground. 217 | if(isCancelled()){ 218 | return null; 219 | } 220 | albumId = params[0]; 221 | // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist. 222 | Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId); 223 | Bitmap bmp = null; 224 | try { 225 | // Decode the bitmap. 226 | bmp = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), albumArt); 227 | // Create a scalled down version of the bitmap to be more memory efficient. 228 | // THe smaller the bitmap the more items we can store inside of the LRU cache. 229 | bmp = Bitmap.createScaledBitmap(bmp, width, height, true); 230 | } catch (IOException e) { 231 | e.printStackTrace(); 232 | } 233 | return bmp; 234 | } 235 | } 236 | /** 237 | * Custom drawable that holds a LoadArtworkTask 238 | */ 239 | private static class AsyncDrawable extends BitmapDrawable { 240 | WeakReference loadArtworkTaskWeakReference; 241 | 242 | public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) { 243 | super(resources, bitmap); 244 | // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected. 245 | loadArtworkTaskWeakReference = new WeakReference(task); 246 | } 247 | 248 | public LoadAlbumArt getLoadArtworkTask() { 249 | return loadArtworkTaskWeakReference.get(); 250 | } 251 | } 252 | 253 | @Override 254 | public int getItemCount() { 255 | return mMusic.size(); 256 | } 257 | 258 | /** 259 | * Custom ViewHolder that represents the List Item. 260 | */ 261 | public static class MusicViewHolder extends RecyclerView.ViewHolder { 262 | 263 | ImageView icon; 264 | TextView title; 265 | TextView artist; 266 | 267 | public MusicViewHolder(View itemView) { 268 | super(itemView); 269 | icon = (ImageView) itemView.findViewById(R.id.icon); 270 | title = (TextView) itemView.findViewById(R.id.title); 271 | artist = (TextView)itemView.findViewById(R.id.subtitle); 272 | } 273 | } 274 | } 275 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/java/com/simpleware/jonathan/musicloaderexample/MusicLoader.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | import android.content.ContentResolver; 4 | import android.content.Context; 5 | import android.database.ContentObserver; 6 | import android.database.Cursor; 7 | import android.os.Handler; 8 | import android.provider.MediaStore; 9 | import android.support.v4.content.AsyncTaskLoader; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by JDavis on 7/26/2016. 16 | */ 17 | public class MusicLoader extends AsyncTaskLoader> { 18 | 19 | List mCache; 20 | MusicObserver mMusicObserver; 21 | 22 | public MusicLoader(Context context) { 23 | super(context); 24 | } 25 | 26 | @Override 27 | public List loadInBackground() { 28 | final ContentResolver contentResolver = getContext().getContentResolver(); 29 | String [] projections = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID}; 30 | String selection = MediaStore.Audio.Media.IS_MUSIC + " =1"; 31 | String sortOrder = MediaStore.Audio.Media.TITLE + " ASC"; 32 | Cursor cr = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projections, selection, null, sortOrder); 33 | List items = new ArrayList<>(); 34 | if(cr != null && cr.moveToFirst()) { 35 | // Cache the column indexes so we don't have to look them up for every iteration of the do-while loop. 36 | int idIndex = cr.getColumnIndex(MediaStore.Audio.Media._ID); 37 | int titleIndex = cr.getColumnIndex(MediaStore.Audio.Media.TITLE); 38 | int artistIndex = cr.getColumnIndex(MediaStore.Audio.Media.ARTIST); 39 | int albumId = cr.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); 40 | do { 41 | if(isLoadInBackgroundCanceled()){ 42 | return items; 43 | } 44 | // Music object to hold the music data. 45 | Music item = new Music(); 46 | // Retrieve the respective music data from the cursor using the column index. 47 | item.setId(cr.getLong(idIndex)); 48 | item.setTitle(cr.getString(titleIndex)); 49 | item.setArtist(cr.getString(artistIndex)); 50 | item.setAlbumId(cr.getLong(albumId)); 51 | // Once we've loaded the Music object, store it inside of the arraylist. 52 | items.add(item); 53 | } 54 | while(cr.moveToNext()); 55 | cr.close(); 56 | 57 | } 58 | return items; 59 | } 60 | 61 | @Override 62 | public void deliverResult(List data) { 63 | if(isReset()){ 64 | 65 | // CLose cursors or databse handles. 66 | return; 67 | } 68 | // Keep a reference to the loaded music data. 69 | mCache = data; 70 | 71 | // If we are started pass the loaded music to our super implementation that handles sending it to the registered activity/fragment. 72 | if(isStarted()){ 73 | super.deliverResult(data); 74 | } 75 | } 76 | 77 | @Override 78 | protected void onStopLoading() { 79 | cancelLoad(); 80 | } 81 | 82 | @Override 83 | protected void onStartLoading() { 84 | if(mCache != null) { 85 | deliverResult(mCache); 86 | } 87 | 88 | if(mMusicObserver == null) { 89 | mMusicObserver = new MusicObserver(this, new Handler()); 90 | getContext().getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mMusicObserver); 91 | } 92 | 93 | if(takeContentChanged() || mCache == null) { 94 | forceLoad(); 95 | } 96 | } 97 | 98 | @Override 99 | protected void onReset() { 100 | // Close any cursors, web-sockets or database objects 101 | if(mMusicObserver != null) { 102 | getContext().getContentResolver().unregisterContentObserver(mMusicObserver); 103 | mMusicObserver = null; 104 | } 105 | } 106 | 107 | /** 108 | * Simple observer that notifies the loader when it has detected a change. 109 | */ 110 | private static class MusicObserver extends ContentObserver { 111 | 112 | private android.support.v4.content.Loader mLoader; 113 | 114 | public MusicObserver(android.support.v4.content.Loader loader, Handler handler) { 115 | super(handler); 116 | mLoader = loader; 117 | } 118 | 119 | @Override 120 | public void onChange(boolean selfChange) { 121 | // A change has been detectec notify the Loader. 122 | mLoader.onContentChanged(); 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/drawable-hdpi/ic_music_note_black_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/drawable-hdpi/ic_music_note_black_48dp.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/drawable-mdpi/ic_music_note_black_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/drawable-mdpi/ic_music_note_black_48dp.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/drawable-xhdpi/ic_music_note_black_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/drawable-xhdpi/ic_music_note_black_48dp.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/drawable-xxhdpi/ic_music_note_black_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/drawable-xxhdpi/ic_music_note_black_48dp.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/drawable-xxxhdpi/ic_music_note_black_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/drawable-xxxhdpi/ic_music_note_black_48dp.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/layout/activity_loader.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/layout/list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | 18 | 25 | 32 | 39 | 40 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FF5722 4 | #E64A19 5 | #FFCCBC 6 | #448AFF 7 | #212121 8 | #727272 9 | #FFFFFF 10 | 11 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MusicLoaderExample 3 | 4 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /MusicLoaderExample/app/src/test/java/com/simpleware/jonathan/musicloaderexample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.musicloaderexample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /MusicLoaderExample/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /MusicLoaderExample/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /MusicLoaderExample/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CincyAndroiDeveloper/Android-Tutorials/f3d89ffb55004a83daec4003c5bb09d796666aa8/MusicLoaderExample/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /MusicLoaderExample/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /MusicLoaderExample/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /MusicLoaderExample/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /MusicLoaderExample/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "21.1.2" 6 | 7 | defaultConfig { 8 | applicationId "com.simpleware.jonathan.listviewexample" 9 | minSdkVersion 19 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile 'com.android.support:appcompat-v7:22.1.0' 24 | } 25 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Jonathan\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/simpleware/jonathan/listviewexample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.listviewexample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/simpleware/jonathan/listviewexample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.simpleware.jonathan.listviewexample; 2 | 3 | import android.content.Context; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.LayoutInflater; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.AdapterView; 13 | import android.widget.ArrayAdapter; 14 | import android.widget.Button; 15 | import android.widget.ImageView; 16 | import android.widget.ListView; 17 | import android.widget.TextView; 18 | import android.widget.Toast; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | 24 | public class MainActivity extends AppCompatActivity { 25 | 26 | private ArrayList data = new ArrayList(); 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_main); 32 | ListView lv = (ListView) findViewById(R.id.listview); 33 | generateListContent(); 34 | lv.setAdapter(new MyListAdaper(this, R.layout.list_item, data)); 35 | lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 36 | @Override 37 | public void onItemClick(AdapterView parent, View view, int position, long id) { 38 | Toast.makeText(MainActivity.this, "List item was clicked at " + position, Toast.LENGTH_SHORT).show(); 39 | } 40 | }); 41 | } 42 | 43 | private void generateListContent() { 44 | for(int i = 0; i < 55; i++) { 45 | data.add("This is row number " + i); 46 | } 47 | } 48 | 49 | @Override 50 | public boolean onCreateOptionsMenu(Menu menu) { 51 | // Inflate the menu; this adds items to the action bar if it is present. 52 | getMenuInflater().inflate(R.menu.menu_main, menu); 53 | return true; 54 | } 55 | 56 | @Override 57 | public boolean onOptionsItemSelected(MenuItem item) { 58 | // Handle action bar item clicks here. The action bar will 59 | // automatically handle clicks on the Home/Up button, so long 60 | // as you specify a parent activity in AndroidManifest.xml. 61 | int id = item.getItemId(); 62 | 63 | //noinspection SimplifiableIfStatement 64 | if (id == R.id.action_settings) { 65 | return true; 66 | } 67 | 68 | return super.onOptionsItemSelected(item); 69 | } 70 | 71 | private class MyListAdaper extends ArrayAdapter { 72 | private int layout; 73 | private List mObjects; 74 | private MyListAdaper(Context context, int resource, List objects) { 75 | super(context, resource, objects); 76 | mObjects = objects; 77 | layout = resource; 78 | } 79 | 80 | @Override 81 | public View getView(final int position, View convertView, ViewGroup parent) { 82 | ViewHolder mainViewholder = null; 83 | if(convertView == null) { 84 | LayoutInflater inflater = LayoutInflater.from(getContext()); 85 | convertView = inflater.inflate(layout, parent, false); 86 | ViewHolder viewHolder = new ViewHolder(); 87 | viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail); 88 | viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text); 89 | viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn); 90 | convertView.setTag(viewHolder); 91 | } 92 | mainViewholder = (ViewHolder) convertView.getTag(); 93 | mainViewholder.button.setOnClickListener(new View.OnClickListener() { 94 | @Override 95 | public void onClick(View v) { 96 | Toast.makeText(getContext(), "Button was clicked for list item " + position, Toast.LENGTH_SHORT).show(); 97 | } 98 | }); 99 | mainViewholder.title.setText(getItem(position)); 100 | 101 | return convertView; 102 | } 103 | } 104 | public class ViewHolder { 105 | 106 | ImageView thumbnail; 107 | TextView title; 108 | Button button; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 14 | 15 | 21 | 22 |