diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-02-19 20:03:36 +0100 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-02-19 20:03:36 +0100 |
commit | 83aca7a0d44ede3960fc5752a205a55f63321832 (patch) | |
tree | 3f8c599746ce3863c4c341d00ef958bf1d42da2e /app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt | |
parent | 2aaa50c8303018f89da461750d5577902bc87357 (diff) | |
parent | 485d3ca989bd4cce1788eae17bcc7587d5ef2c90 (diff) |
Merge branch 'release/1.1'v1.1.0
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt')
-rw-r--r-- | app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt new file mode 100644 index 0000000..18fd7a0 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt @@ -0,0 +1,70 @@ +@file:Suppress("DEPRECATION") + +package xyz.adjutor.aniki.topmanga + +import android.annotation.SuppressLint +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.os.AsyncTask +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import xyz.adjutor.aniki.R + +class TopMangaAdapter(val mangaList: List<TopManga>) : + RecyclerView.Adapter<TopMangaAdapter.MangaViewHolder>() { + + // Describes an item view and its place within the RecyclerView + class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val mangaTitle: TextView = itemView.findViewById(R.id.tv_title) + val mangaRank: TextView = itemView.findViewById(R.id.tv_rank) + val mangaImage: ImageView = itemView.findViewById(R.id.iv_image) + + } + + // Returns a new ViewHolder + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MangaViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.item_layout, parent, false) + + return MangaViewHolder(view) + } + + // Returns size of data list + override fun getItemCount(): Int { + return mangaList.size + } + + // Displays data at a certain position + override fun onBindViewHolder(holder: MangaViewHolder, position: Int) { + val currentManga: TopManga = mangaList[position] + holder.mangaTitle.text = currentManga.title + holder.mangaRank.text = currentManga.rank.toString() + DownloadImageFromInternet(holder.mangaImage).execute(currentManga.image_url) + } + + @SuppressLint("StaticFieldLeak") + private inner class DownloadImageFromInternet(var imageView: ImageView) : AsyncTask<String, Void, Bitmap?>() { + override fun doInBackground(vararg urls: String): Bitmap? { + val imageURL = urls[0] + var image: Bitmap? = null + try { + val `in` = java.net.URL(imageURL).openStream() + image = BitmapFactory.decodeStream(`in`) + } + catch (e: Exception) { + Log.e("Error Message", e.message.toString()) + e.printStackTrace() + } + return image + } + override fun onPostExecute(result: Bitmap?) { + imageView.setImageBitmap(result) + } + } +} + |