Искать в ListView Kotlin - PullRequest
0 голосов
/ 03 мая 2018

Я работаю над проектом, в котором я получил свой список контактов и передал его в список. Но мне нужно отфильтровать контакты, используя текст редактирования, и я нигде не могу найти решение. Я надеюсь, что кто-то поможет мне, я передам код ниже. Спасибо.

Пользовательский адаптер:

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()
    }
}

Основной класс:

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

    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.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
    }
}

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...