Doubt with Android Studio / Kotlin applying Searchview filter with RecyclerView to query from MySql / PHP

0

As you can see, I'm new to using stack overflow .

I'm using Android Studio with Kotlin. It is not yet clear to me how to get to the filter. I have seen other examples to guide me, but I find no similarity for it. I hope you can help me and I hope that what has been done so far, I can help you get someone else out of your doubts.

I made a RecyclerView filling it from a query to MySql. The activity responds well and fills the RecyclerView perfectly:

But I could not relate where and how to apply the filter.

Here I share my code:

This is the code of my main Activity

  

RecyclerView.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.Menu
import android.widget.LinearLayout
import android.widget.SearchView
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONException
import org.json.JSONObject

class RecyclerView : AppCompatActivity() {
    private var users = ArrayList<User>()
    private var adapter = CustomAdapter(users)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        val rv = findViewById<RecyclerView>(R.id.recyclerView1)
        rv.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        LoadProductores(users, rv)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_busca_productor, menu)
        val item = menu.findItem(R.id.menu_BuscaProd)
        val searchView = item.actionView as SearchView
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return false
            }
            override fun onQueryTextChange(newText: String): Boolean {
                return false
            }
        })
        return super.onCreateOptionsMenu(menu)
    }
// Llamada a servidor desde PHP y MySql
   private fun LoadProductores(users: ArrayList<User>, rv: RecyclerView) {
        val stringRequest = StringRequest(Request.Method.GET,
                EndPoints.URL_GET_PRODUCTORES_UNICOS,
                Response.Listener<String> { s ->
                    try {
                        val obj = JSONObject(s)
                        if (!obj.getBoolean("error")) {
                            val array = obj.getJSONArray("productores")
                            if (array.length()>0) {
                                for (i in 0..array.length() - 1) {
                                    val objectArtist = array.getJSONObject(i)
                                    users.add(User(objectArtist.getString("NombreCompleto")))
                                }
                                rv.adapter = adapter
                             }else{
                                setTitle("No se eonctraron resultados resultados")
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_LONG).show()
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }, Response.ErrorListener { volleyError -> Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show() })
        val requestQueue = Volley.newRequestQueue(this)
        requestQueue.add<String>(stringRequest)
    }

}

The Adapter

  

CustomAdapter.kt

import android.graphics.ColorSpace
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.util.Log
import kotlin.collections.ArrayList


class CustomAdapter(val userList: ArrayList<User>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
        holder.txtName?.text = userList[position].name
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return ViewHolder(v);
    }

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


    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val txtName = itemView.findViewById<TextView>(R.id.txtName)
    }
}

The data class

  

User.kt

data class User(val name: String)

The Layout of the main activity

  

activity_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<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="net.agrosoft.segalg.RecyclerView">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>

The Layout MenuSearch

  

menu_busca_productor.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_BuscaProd"
        android:title="Buscar"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always">
    </item>
</menu>

In advance, thanks for your contributions.

    
asked by Antonio -Tano- Higuera 27.04.2018 в 02:25
source

0 answers