Hi Everyone, I'm rather new to Kotlin programming ...
# apollo-kotlin
r
Hi Everyone, I'm rather new to Kotlin programming for Android, and I am learning graphql with Apollo. I just am trying to have a simple anime character image query where you can search any of the anime characters and return their image. I have been stuck for a while after reviewing many resources and videos. I would appreciate any feedback/help in the right direction. Here is my Picture.graphql code below
Copy code
query Picture($search: String) {
    person: Character(search: $search) {
        image {
            medium
        }
    }
}
I am still trying to understand how to execute the query and connect my query to the UI. I'm trying to follow the tutorial, but am getting stuck still. Please see my MainActivity.kt code below
Copy code
package com.example.thisone

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.SearchView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.exception.ApolloException
import com.example.thisone.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {


    val apolloClient = ApolloClient.Builder()
        .serverUrl("<https://graphql.anilist.co>")
        .build()



    lateinit var binding: ActivityMainBinding

    override fun onCreateView(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

val UserAdapter : ArrayAdapter<String> = ArrayAdapter(
    this,android.R.layout.simple_list_item_1,
    user

)
        binding.userList.adapter = userAdapter



        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            lifecycleScope.launchWhenResumed {
                val response = try {
                    apolloClient.query(PictureQuery()).execute()
                } catch (e: ApolloException) {
                    Log.d("Anime Character Picture", "Success", e)
                    null
                }
                val images = response?.data?.person?.image?.filterNotNull()
                if (images != null && !response.hasErrors()) {
                    val adapter = PictureQuery_ResponseAdapter(image)
                    binding.image.layoutManager = LinearLayoutManager(requireContext())
                    binding.launches.adapter = adapter

                }

            }

            


            binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                binding.searchView.clearFocus()
                if (user.contains(query)) {
                    userAdapter.filter.filter(query)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                userAdapter.filter.filter(newText)
                return false
            }
        })


    }

}
I also made a PictureAdapter.kt file where I'll show the code below, but I am unsure if everything I'm doing is setting me on the right track or if I'm completely in error.
Copy code
package com.example.rocketreserver

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.thisone.PictureQuery
import com.example.thisone.databinding.ActivityMainBinding

class PictureAdapter(
    private val images: List<PictureQuery.Person>
) : RecyclerView.Adapter<PictureAdapter.ViewHolder>() {

    class ViewHolder(val binding: ActivityMainBinding) : RecyclerView.ViewHolder(binding.root)


    override fun getItemCount(): Int {
        return images.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ActivityMainBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val person = images[position]
        holder.binding.userList.textAlignment = person.image ?: ""

    }
}
Here is the activity_main.xml code below. Again, thank you in advance for helping a super novice programmer. I am slowly but surely learning my way.
Copy code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:app="<http://schemas.android.com/apk/res-auto>"
    xmlns:tools="<http://schemas.android.com/tools>"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Enter Anime Character Name"
        android:iconifiedByDefault="false"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/searchview_background"
        android:queryBackground="@android:color/background_light"/>

    <ListView
        android:id="@+id/userList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"/>
</LinearLayout>
m
This looks about right overall. You'll certainly want to set something else than textAlignment in your adapter. If your image is an url, I'd look into image loading libraries like coil or glide