Creare un custom adapter JSON in Android, Kotlin e Volley

Mattepuffo's logo
Creare un custom adapter JSON in Android, Kotlin e Volley

Creare un custom adapter JSON in Android, Kotlin e Volley

Modifiche post suggerimenti forum.html.it

Oggi vediamo come creare un custom adapter in Kotlin e Android, prendendo i dati da un JSON remoto.

Il JSON è liberamente accessibile; potete vedere l'url nel codice.

Con i dati andremo a riempire una ListView.

Premetto che:

  • per le richieste HTTP useremo Volley (qui trovate un tutorial)
  • per il parsing del JSON non useremo librerie esterne

Cominciamo con il layout, partendo dal layout della Activity (main_activity.xml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

All'interno c'è solo la ListView; questo il suo layout (list_row.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:weightSum="1"
    android:gravity="center_vertical"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

Adesso andiamo a creare il nostro Adapter:

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import java.util.*
import kotlin.collections.ArrayList

class BookAdapter(
    private val context: Context,
    private val dataList: ArrayList<Book>
) : BaseAdapter() {

    val inflater: LayoutInflater =
        this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
    var tempNameVersionList = ArrayList(dataList)

    override fun getCount(): Int {
        return dataList.size;
    }

    override fun getItem(position: Int): Book {
        return dataList.get(position);
    }

    override fun getItemId(position: Int): Long {
        return position.toLong();
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val rowView = inflater.inflate(R.layout.list_row, parent, false);
        var item: Book = getItem(position);
        rowView.findViewById<TextView>(R.id.txtTitle).text = item.title;
        rowView.tag = position;
        return rowView;
    }

    fun filter(text: String?) {
        val text = text!!.toLowerCase(Locale.getDefault());
        dataList.clear();

        if (text.length == 0) {
            dataList.addAll(tempNameVersionList);
        } else {
            for (b: Book in tempNameVersionList) {
                if (b.title.toLowerCase().contains(text)) {
                    dataList.add(b);
                }
            }
        }
        notifyDataSetChanged();
    }
}

Come già detto, la richiesta alla API viene fatta con Volley,

Vi conviene rivedere l'altro tutorial che spiega esattamente cosa fa.


Condividi

Commentami!