3

So I am working with Kotlin on this project, I mainly use Java so I'm trying to find equivalents in Kotlin to achieve the same functionality.

I am getting a JSONArray from my Database and storing it in a Serializable data class called Congregation it has the following variables id: Int, name: String, language: String

I now have a registration activity in which there is a "Congregation" input, I decided to make this an AutoCompleteTextView so that I could suggest possible values matching what the users typing. I created a custom ArrayAdapter and it works fine when displaying the name of a Congregation (image below)

Emulator showing RegisterActivity.java

However, when I select one of those values, it displays the full value text from the list

e.g. If I select "Leeds, Crossgates (English)" in the input it will display Congregation(id=1, name=Leeds, Crossgates, language=English)

I am wondering how I can keep the name value of a Congregation in the box when it is selected.

Also when I try to delete the current value of the box after selecting an item I receive an IndexOutOfBoundsException Index: 1 Size: 1

Custom Array Adapter (CongregationListAdapter.kt):

class CongregationListAdapter(context: Activity, resourceId: Int, textView: Int, private var congregations: List<Congregation>)
:ArrayAdapter<Congregation>(context, resourceId, textView, congregations) {

private var inflater: LayoutInflater = context.layoutInflater
private lateinit var view: View

@SuppressLint("SetTextI18n")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

    if (convertView == null) {
        view = inflater.inflate(R.layout.congregation_list_item, parent, false)
    }
    val name: TextView = view.findViewById(R.id.congregation_text)

    val congregation: Congregation? = getItem(position)
    if (congregation != null) {
        name.text = congregation.name + " (" + congregation.language + ")"
    } else {
        name.text = ""
        return view
    }
    return view
}

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

@SuppressLint("SetTextI18n")
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
    if (convertView == null) {
        view = inflater.inflate(R.layout.congregation_list_item, parent, false)
    }
    val congregation: Congregation? = getItem(position)
    val name: TextView = view.findViewById(R.id.congregation_text)
    if (congregation != null) {
        name.text = congregation.name + " (" + congregation.language + ")"
    } else {
        name.text = "Oops. There was a problem"
    }
    return view
}

}

Custom View (congregation_list_item.xml):

<?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="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/congregation_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="Leeds, Crossgates" />

</android.support.constraint.ConstraintLayout>

</LinearLayout>

Conregation Model (Congregation.kt):

@kotlinx.serialization.Serializable
data class Congregation(
    var id: Int,
    var name: String,
    var language: String
)
Mwikala Kangwa
  • 430
  • 7
  • 24
  • You should use a local variable into `getView()` and `getDropDownView()` instead of a class variable like you did for `view` – Blo Feb 05 '19 at 13:39
  • a local variable for what exactly, sorry? – Mwikala Kangwa Feb 05 '19 at 13:43
  • You should do something like: `override getView(...) { val view: View = convertView ?: inflater.inflate(...) }` and return it at the end. Same thing for `getDropDownView()`. This can prevent some weird behviors I guess. This will also replace your `if null` check ;) – Blo Feb 05 '19 at 13:47
  • is this possible to use `getAdapterPosition` instead? – mochadwi Dec 25 '19 at 06:35

1 Answers1

0

You can simply implement toString in the Congregation class:

@kotlinx.serialization.Serializable
data class Congregation(
    var id: Int,
    var name: String,
    var language: String
) {
    override fun toString(): String {
        return "$name ($language)"
    }
}
Chenglong Ma
  • 345
  • 3
  • 13