Search in ListView Kotlin

0

I am working on a project in which I obtained my contact list and have passed them to a listview. But I need to filter the contacts using an edittext and I can not find any solution anywhere. I hope someone helps me, I pass the code below. Thanks.

Custom Adapter:

class ContactsAdapter(private val context: Context, private val 
contactModelArrayList: ArrayList<ContactModel>) : BaseAdapter() {

override fun getViewTypeCount(): Int {
    return count
}

override fun getItemViewType(position: Int): Int {

    return position
}

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

override fun getItem(position: Int): Any {
    return contactModelArrayList[position]
}

override fun getItemId(position: Int): Long {
    return 0
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    var convertView = convertView
    val holder: ViewHolder

    if (convertView == null) {
        holder = ViewHolder()
        val inflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        convertView = inflater.inflate(R.layout.contacts, null, true)

        holder.tvname = convertView!!.findViewById(R.id.name) as TextView


        convertView.tag = holder
    } else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = convertView.tag as ViewHolder
    }

    holder.tvname!!.setText(contactModelArrayList[position].getNames())


    return convertView
}

private inner class ViewHolder {

    var tvname: TextView? = null


}

}

ContactModel:

class ContactModel {

var name: String? = null


fun setNames(name: String) {
    this.name = name
}

fun getNames(): String {
    return name.toString()
}

}

Main Class:

class RecentsSearch : AppCompatActivity() {
private var listView: ListView? = null
private var customAdapter: ContactsAdapter? = null
private var contactModelArrayList: ArrayList<ContactModel>? = null
private var et_search : EditText? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_recents_search)

    listView= findViewById(R.id.listViewSearch) as ListView
    et_search= findViewById(R.id.searchText) as EditText
    contactModelArrayList = ArrayList()

    val phones = 
contentResolver.query(ContactsContract.CommonDataKinds.Phone.
CONTENT_URI, null, null, null, 
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC")
    while (phones!!.moveToNext()) {
        val name =  
phones.getString(phones.getColumnIndex(ContactsContract.
CommonDataKinds.Phone.DISPLAY_NAME))
        val contactModel = ContactModel()
        contactModel.setNames(name)
        contactModelArrayList!!.add(contactModel)
    }
    phones.close()

    customAdapter = ContactsAdapter(this, contactModelArrayList!!)
    listView!!.adapter = customAdapter

    et_search!!.addTextChangedListener(object : TextWatcher{
        override fun afterTextChanged(s: Editable?) {
            listView!!.setFilterText(s.toString())
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, 
count: Int, after: Int) {
            listView!!.setFilterText(s.toString())            }

        override fun onTextChanged(s: CharSequence?, start: Int, 
before: Int, count: Int) {
                      }

    })
}
}

activity.xml

<android.support.constraint.ConstraintLayout 
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"
tools:context=".RecentsSearch"
android:background="@drawable/fondo_gradient">

<ImageView
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:src="@drawable/logosm"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintVertical_bias="0.03"/>

<ImageButton
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/presence_online"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.02"
    app:layout_constraintHorizontal_bias="0.95"
    android:scaleType="fitCenter"
    android:background="@android:color/transparent"
    />

<EditText
    android:id="@+id/searchText"
    android:layout_width="280dp"
    android:layout_height="40dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.1"
    android:backgroundTint="@color/colorTurquoise"
    android:textAlignment="center"/>

<ImageView
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:src="@drawable/magnifier"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.112"
    app:layout_constraintHorizontal_bias="0.16"/>

<ImageView
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:src="@drawable/ic_close"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.119"
    app:layout_constraintHorizontal_bias="0.83"/>

<ImageView
    android:layout_width="50dp"
    android:layout_height="60dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.19"
    app:layout_constraintHorizontal_bias="0.05"
    android:src="@drawable/contacts"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="25dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.218"
    app:layout_constraintHorizontal_bias="0.456"
    android:textSize="20sp"
    android:textColor="@color/colorTurquoise"/>

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.31"
    app:layout_constraintHorizontal_bias="0.05"
    android:src="@drawable/ic_group"/>
<TextView
    android:layout_width="200dp"
    android:layout_height="25dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.32"
    app:layout_constraintHorizontal_bias="0.6"
    android:textSize="20sp"
    android:text="Create group"
    android:textColor="@color/colorTurquoise"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="370dp"
    android:id="@+id/listViewSearch"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1"
    android:listSelector="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:divider="@null"
    android:dividerHeight="10dp"
    android:elevation="100dp"
    android:focusableInTouchMode="false"
    android:isScrollContainer="false"
    />

</android.support.constraint.ConstraintLayout>

contacts.xml

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="beforeDescendants">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_background_selector"
    android:descendantFocusability="afterDescendants">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/contacts"
        />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="23dp"
        android:text="Name"/>

</LinearLayout>
</FrameLayout>
    
asked by Jorge Riveiro 03.05.2018 в 11:07
source

2 answers

0

In this lambda the third parameter indicates the position of the selected item in the ListView. With this value we can locate the respective value in the settlement of inhabitants.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val paises = arrayOf( "Argentina", "Chile", "Paraguay", "Bolivia", "Peru", "Ecuador", "Brasil", "Colombia", "Venezuela", "Uruguay")
    var habitantes = arrayOf(40_000_000, 17_000_000, 6_500_000, 10_000_000, 30_000_000, 14_000_000, 183_000_000, 44_000_000, 31_000_000, 3_500_000)
    val adaptador1 = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, paises)
    list1.adapter = adaptador1
    list1.setOnItemClickListener { adapterView, view, i, l ->
        tv1.text = "Población de: ${habitantes[i]}"
    }
}
    
answered by 18.09.2018 в 22:15
0

When we use a editText , the user can write a search parameter and we compare that result with the words in the list, which is nothing more than a ArrayList , so I would do a method where you pass the string of the user and what you return is a new arraylist with the matches, and that you pass it to the adapter.

I leave you a very simple example in Kotlin that you have to adapt to your needs, as can be the parameters of the function:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val wordToSearch = "example1"
        val possibleResults: List<String> = listOf("example1", "example2", "example3", "example4")

        getUserCoincidence(wordToSearch, possibleResults).forEach {
            Log.i("test", it)
        }
    }

    private fun getUserCoincidence(wordToSearch: String, possibleResults: List<String>): List<String>
            = possibleResults.filter { it == wordToSearch }
}

The getUserCoincidence() method returns a List<String> with the matches, that list is passed to the adapter. In the example you can check that the log shows the matches that will appear on your list.

    
answered by 17.09.2018 в 17:09