How To Search data from RecyclerView Retrofit Kotlin Android Studio? - android-studio

How to search for data in RecyclerView?
I have model like this and I get data with retrofit. I have to make RecyclerView with model data like this and how to make Search from RecyclerView?
data class ListArticle(
#SerializedName("status") val status : String,
#SerializedName("totalResults") val totalResults : Int,
#SerializedName("articles") val articles : List<Articles>
)
data class Source (
#SerializedName("id") val id : String,
#SerializedName("name") val name : String
)
data class Articles (
#SerializedName("source") val source : Source,
#SerializedName("author") val author : String,
#SerializedName("title") val title : String,
#SerializedName("description") val description : String,
#SerializedName("url") val url : String,
#SerializedName("urlToImage") val urlToImage : String,
#SerializedName("publishedAt") val publishedAt : String,
#SerializedName("content") val content : String
)

Have to solve this question and I'm sorry for not completed question
just added this to your adapter
fun getFilter(): Filter? {
return articleFilter
}
private val articleFilter: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults? {
val filteredList: MutableList<Articles> = ArrayList()
if (constraint == null || constraint.length == 0) {
filteredList.addAll(articles)
} else {
val filterPattern =
constraint.toString().toLowerCase().trim { it <= ' ' }
for (item in articles) {
if (item.title.toLowerCase().contains(filterPattern)) {
filteredList.add(item)
}
}
}
val results = FilterResults()
results.values = filteredList
return results
}
override fun publishResults(
constraint: CharSequence?,
results: FilterResults
) {
searchArticles = results.values as List<Articles>
notifyDataSetChanged()
}
}
fun setData(article: List<Articles>){
searchArticles = article
notifyDataSetChanged()
}

Related

update one particukar item in recyclerview from fragment after success response android kotlin

Fragment Recyclerview code :
private fun initRecyclerView() {
bestSellersAdapter = BestSellerProductsAdapter(frag)
homeBinding.popularproductsRView.apply {
bestSellersAdapter.second(this#HomeFragment)
adapter = bestSellersAdapter
}
}
private fun getSection() {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
roomProductVM.productFlow.collectLatest {
if (it.isEmpty()) {
categoryViewModel.getAllCategories()
} else {
bestSellersAdapter.setMutableArraylist(it)
bestSellersAdapter.notifyDataSetChanged()
}
}
}
}
based on condition i call this function(getSection) in onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initRecyclerView()
getSection()
addcartviewModel.cartTableSuccessorNot.observe(viewLifecycleOwner) {
if(it.toInt() == variant_id) {
Toast.makeText(context,"Product Updated Successfully",Toast.LENGTH_SHORT).show() homeBinding.popularproductsRView.adapter?.notifyItemChanged(productPosition)
} else {
LoadingUtils.hideDialog()
Toast.makeText(context,"Failure in Cart Table", Toast.LENGTH_SHORT).show()
}
}
}
interface function in fragment : ( here i am taking product position from adapter )
override fun updatepro(position: Int, type: String, id: Int, cart_count: Int, is_notify: Boolean, product_id: Int, name: String, measurement: String,
variant_image: String, price: String, qty: String, stock: String, image: String, moq: String, unit: String) {
productPosition = position
variant_id = id
variant_qty = qty.toInt()
addcartviewModel.updateVariantTableinRoom(id,cart_count.toString(),is_notify,product_id)
addcartviewModel.insertVariantListintoCartTable(id,measurement,price,stock,image,moq,name,variant_image,unit,qty)
}
Adapter class :
class BestSellerProductsAdapter(var context: Context) : RecyclerView.Adapter<BestSellerProductsAdapter.BestSellerViewHolder>() {
var mutableList = mutableListOf<ProductWithVariants>()
fun setMutableArraylist(datas: MutableList<ProductWithVariants>) {
mutableList.clear()
mutableList.addAll(datas)
}
override fun onBindViewHolder(holder: BestSellerViewHolder, position: Int) {
var cart_count = mutableList[position].variantsList[0].cart_count
if(stock?.toInt()==0) {
holder.notify.visibility = View.VISIBLE
holder.addtocart.visibility = View.GONE
holder.pll_cart.visibility = View.GONE
} else if(cart_count == 0) {
holder.addtocart.visibility = View.VISIBLE
holder.pll_cart.visibility = View.GONE
holder.notify.visibility = View.GONE
} else {
holder.notify.visibility = View.GONE
holder.addtocart.visibility = View.GONE
holder.pll_cart.visibility = View.VISIBLE
holder.tv_cart.text = cart_count.toString()
}
holder.addtocart.setOnClickListener {
cart_count = ((cart_count ?: 0) + 1)
selectpro?.updatepro(position,"AddCart",mutableList[position].variantsList[0].id,mutableList[position].variantsList[0].cart_count!!.toInt(),false,mutableList[position].products.id,
mutableList[position].products.name.toString(),
mutableList[position].variantsList[0].measurement.toString() + mutableList[position].variantsList[0].measurement_unit_name.toString(),
mutableList[position].variantsList[0].image.toString(),
mutableList[position].variantsList[0].discounted_price.toString(),
cart_count.toString(),
stock.toString(),
mutableList[position].variantsList[0].image.toString(),
moq.toString(),
mutableList[position].variantsList[0].measurement_unit_name.toString()
)
}
var selectpro: selectproduct?=null
fun second(selectproduct: selectproduct) {
selectpro = selectproduct
}
interface selectproduct {
fun updatepro(position:Int,type:String,id:Int, cart_count:Int,is_notify:Boolean,product_id:Int,
name:String,measurement:String,variant_image:String,price:String,qty:String,
stock:String,image:String,moq:String,unit:String)
fun selectProduct(product: ProductWithVariants,position:Int)
}
Now i want to update the particular item of recycler view after Toast ( Product Updated Successfully ) in fragment .. how to do that
1 After response first find out the item position you want to update
in adapter
2 Also you have to update the list in adapter with new list of data
you get from the response.
then use below code to update particular item in adapter using
position.
adapter.notifyItemChanged(position)

main adapter not submitting paging data android kotlin

Main Fragment:
here were initializing the recyclerview and the viewmodel and getting the data to submit to the adapter:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val binding: FragmentMainBinding = FragmentMainBinding.inflate(inflater, container, false)
this.binding = FragmentMainBinding.inflate(layoutInflater)
dataFromViewModel = arrayListOf()
setupViewModel()
setupList()
setupView()
return binding.root;
}
private fun setupView() {
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
viewModel.listData.collectLatest {
Log.e("Output Frg", it.toString())
if (!dataFromViewModel.contains(it)) {
dataFromViewModel.add(it)
mainAdapter.submitData(it)
mainAdapter.notifyDataSetChanged()
Log.e(TAG, "setupView: ${mainAdapter.repos.toString()}", )
}
}
}
}
private fun setupList() {
mainAdapter = MainAdapter()
val gridLayoutManager: GridLayoutManager = GridLayoutManager(context, 2)
binding.recyclerview.apply {
layoutManager = gridLayoutManager
adapter = mainAdapter
}
// mainAdapter.addLoadStateListener { loadState ->
//
// if (loadState.refresh is LoadState.Loading && !flag) {
// binding.progressbar.visibility = View.VISIBLE
// flag = true
// } else {
// binding.progressbar.visibility = View.GONE
// }
// }
}
private fun setupViewModel() {
viewModel =
ViewModelProvider(
this,
ViewModelFactory(RetrofitService.getApiService())
)[MainViewModel::class.java]
}
Main ViewModel:
here were just calling the postdatasource function and adding it to the listData value
class MainViewModel(private val apiService: RetrofitService) : ViewModel() {
val listData = Pager(PagingConfig(pageSize = 6)) {
PostDataSource(apiService)
}.flow.cachedIn(viewModelScope)
}
PostDataSource:
Here, we have extended PostDataSource with PagingSource which will implement a suspend load function which will help us to load the data.
PostDataSource also takes a primary constructor parameter APIService. PostDataSource acts here as a repository and the load function gets the data from the API.
Since the load function is a suspend function, we can call other suspend functions inside it without any issues which we created in APIService.
In the PostDataSource, we take two parameters one of integer type and other of the data type we have to load on the list item. The integer parameter represents the page number here.
Here, we get the page number from params and assign it to nextPage variable using param.key and if it returns null, we set a default value 1.
We also do the API call and get assign the response to the response variable using APIService which we passed as a constructor parameter to PostDataSource class.
After doing all the operations with the successful response, we return the LoadResult.Page object here with the required data and it something went wrong we use LoadResult.Error.
We are also passing null as the next key if there is no corresponding data in that direction.
class PostDataSource(private val apiService: RetrofitService) : PagingSource<Int, repo>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, repo> {
try {
val currentLoadingPageKey = params.key ?: 1
val response = apiService.getListData(currentLoadingPageKey)
val responseData = mutableListOf<repo>()
var repos : List<repo>
val data = response
responseData.addAll(data)
Log.e(TAG, "load: $responseData", )
val prevKey = if (currentLoadingPageKey == 1) null else currentLoadingPageKey - 1
return LoadResult.Page(
data = responseData,
prevKey = prevKey,
nextKey = currentLoadingPageKey.plus(1)
)
} catch (e: Exception) {
return LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, repo>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
// fun getAllrepos() = retrofitService.getAllrepos()
}
MainAdapter:
Now, we will update the MainListAdapter. We will extend the MainListAdapter with PagingDataAdapter and the PagingDataAdapter will take the type of data we need to display in the list and the ViewHolder.
It also takes a DiffUtil callback, as a parameter to its primary constructor which helps the PagingDataAdapter to update the items if they are changed or updated. And DiffUtil callback is used because they are more performant.
Now, the MainListAdapter looks like,
class MainAdapter : PagingDataAdapter<repo, MainAdapter.ViewHolder>(DiffCallBack) , Filterable {
private lateinit var Repos : List<repo>
class ViewHolder(view: View, mlistener: onItemClickListener) : RecyclerView.ViewHolder(view)
private lateinit var mlistener: onItemClickListener
interface onItemClickListener {
fun OnItemClick(position: Int)
}
var repos: MutableList<repo> = ArrayList()
private var reposFull: MutableList<repo> = ArrayList()
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.findViewById<TextView>(R.id.title).text = getItem(position)?.name
Log.e(TAG,"name ${holder.itemView.findViewById<TextView>(R.id.title).text}", )
Glide.with(holder.itemView.context)
.load(getItem(position)?.owner?.avatar_url)
.into(holder.itemView.findViewById(R.id.imgCircle))
// val repo = repos[position]
//
//
// val mainViewHolder: ViewHolder = holder as ViewHolder
// mainViewHolder.itemView Title?.text = repo.name
// mainViewHolder.Image?.let {
// Glide.with(holder.itemView.context).load(repo.owner.avatar_url).into(
// it
// )
// }
}
fun submitList(list: PagingData<repo>) {
Repos = list as List<repo>
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.repo_layout, parent, false), mlistener
)
}
fun setOnItemClickListener(listener: onItemClickListener) {
mlistener = listener
}
object DiffCallBack : DiffUtil.ItemCallback<repo>() {
override fun areItemsTheSame(oldItem: repo, newItem: repo): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: repo, newItem: repo): Boolean {
return oldItem == newItem
}
}
// class MainViewHolder(itemView: View, listener: onItemClickListener) : RecyclerView.ViewHolder(itemView) {
// var Title: TextView? =
// itemView.findViewById<View>(com.lau.google_rep.R.id.title) as TextView
// var Image: ImageView? =
// itemView.findViewById<View>(com.lau.google_rep.R.id.imgCircle) as ImageView
//
// init {
// itemView.setOnClickListener {
// listener.OnItemClick(adapterPosition)
// }
// }
// }
override fun getFilter(): Filter {
return exampleFilter
}
private val exampleFilter: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults? {
val filteredList: MutableList<repo> = ArrayList()
if (constraint == null || constraint.length == 0) {
filteredList.addAll(reposFull)
} else {
val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' }
for (item in reposFull) {
if (item.name.toLowerCase().contains(filterPattern)) {
filteredList.add(item)
}
}
}
Log.d(TAG, "publishResults: $filteredList")
val results = FilterResults()
results.values = filteredList
return results
}
#SuppressLint("NotifyDataSetChanged")
override fun publishResults(constraint: CharSequence?, results: FilterResults) {
repos.clear()
(results.values as? Collection<repo>)?.let { repos.addAll(it) }
notifyDataSetChanged()
}
}
}
retrofit service:
interface RetrofitService {
// #Headers("Authorization: token ")
#GET("repos")
suspend fun getListData(#Query("page") pageNumber: Int): List<repo>
companion object{
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
fun getApiService() = Retrofit.Builder()
.baseUrl("https://api.github.com/orgs/google/")
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
.create(RetrofitService::class.java)
}
}

Error: retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall#94952f

I want to get information using retrofit2
However, it is not possible due to the first error. How to solve this
this error is not infomation,,,,,
data class Beers(
val id : String,
val name : String,
val ph : Int,
val first_brewed : String
)
val retrofit = Retrofit.Builder()
.baseUrl("https://api.punkapi.com/v2/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(BeerService::class.java)
service.getBeers().enqueue(object : Callback<List<Beers>>{
override fun onResponse(call: Call<List<Beers>>, response: Response<List<Beers>>) {
if (response.isSuccessful) {
val body = response.body()
binding.tvRe.text = body.toString()
Log.d("Response :", response?.body().toString())
// body?.let {
//
// }
// Log.d("Response :", response?.body().toString())
}
}
override fun onFailure(call: Call<List<Beers>>, t: Throwable) {
Log.d("Error", call.toString())
}
})
}
I fixed the data class
val ph : Int --> val ph : Float

Retrieve nested Data from Firebase Database android

Snapshot of my firebase realtime database
I want to extract the entire data under the "Orders" node, please tell me how should I model my data class for android in Kotlin?
I tried with this type of modeling,
After getting the reference of (Orders/uid/)
Order.kt
data class Order(
val items:ArrayList<Myitems>=ArrayList(),
val timeStamp:Long=0,
val totalCost:Int=0
)
MyItems.kt
data class MyItems(
val Item:ArrayList<Menu>=ArrayList()
)
Menu.kt
data class Menu(
val menCategory:String="",
val menName:String="",
val menImage:String="",
val menId:String="",
val menQuantity:Int=0,
val menCost:Int=0
)
After a lot of thinking and research online. I was finally able to model my classes and call add value event listener to it. Here it goes:
Order.kt
data class Order(
val items: ArrayList<HashMap<String, Any>> = ArrayList(),
val timeStamp: Long = 0,
val totalCost: Int = 0
)
OItem.kt
data class OItem(
val menCategory: String = "",
val menId: String = "",
val menImage: String = "",
val menName: String = "",
val menPrice: Int = 0,
var menQuantity: Int = 0
)
MainActivity.kt
val uid = FirebaseAuth.getInstance().uid
val ref = FirebaseDatabase.getInstance().getReference("Orders/$uid")
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
//
}
override fun onDataChange(p0: DataSnapshot) {
p0.children.forEach {
val order = it.getValue(Order::class.java)
ordList.add(order!!)
}
Log.d("hf", ordList.toString())
}
})

Same value was passed as the nextKey in two sequential Pages loaded from a PagingSource in Paging Library 3 Android

I migrated from paging 2 to paging 3. I tried to implement ItemKeyedDataSource of Paging 2 to Paging library 3. But the problem I was facing is, the same value(currentJodId) was passed as the nextkey in two sequential Pages loaded. And After that app crashes. but if I add "keyReuseSupported = true" in DataSource, app does not crash. But it started calling same item id as the nextkey.
JobSliderRestApi.kt
#GET("job/list/slides")
fun getDetailOfSelectedJob(
#Query("current_job") currentJodId: Int?,
#Query("limit") jobLimit: Int?,
#Query("search_in") fetchType: String?
): Single<Response<JobViewResponse>>
JobViewResponse.kt
data class JobViewResponse(
#SerializedName("data") val data: ArrayList<JobDetail>?
) : BaseResponse()
JobDetail.kt
data class JobDetail(
#SerializedName("job_id") val jobId: Int,
#SerializedName("tuition_type") val jobType: String?,
#SerializedName("class_image") val jobImage: String,
#SerializedName("salary") val salary: String,
#SerializedName("no_of_student") val noOfStudent: Int,
#SerializedName("student_gender") val studentGender: String,
#SerializedName("tutor_gender") val preferredTutor: String,
#SerializedName("days_per_week") val daysPerWeek: String?,
#SerializedName("other_req") val otherReq: String?,
#SerializedName("latitude") val latitude: Double?,
#SerializedName("longitude") val longitude: Double?,
#SerializedName("area") val area: String,
#SerializedName("tutoring_time") val tutoringTime: String?,
#SerializedName("posted_date") val postedDate: String?,
#SerializedName("subjects") val subjects: String,
#SerializedName("title") val title: String
)
JodSliderDataSource.kt
class JodSliderDataSource #Inject constructor(
private val jobSliderRestApi: JobSliderRestApi
): RxPagingSource<Int, JobDetail>() {
// override val keyReuseSupported = true
#ExperimentalPagingApi
override fun getRefreshKey(state: PagingState<Int, JobDetail>): Int? {
return state.anchorPosition?.let {
state.closestItemToPosition(it)?.jobId
}
}
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, JobDetail>> {
return jobSliderRestApi.getDetailOfSelectedJob(42673, 2, "next").toSingle()
.subscribeOn(Schedulers.io())
.map { jobResponse -> toLoadResult(jobResponse.data) }
.onErrorReturn { LoadResult.Error(it) }
}
private fun toLoadResult(data: ArrayList<JobDetail>): LoadResult<Int, JobDetail> {
return LoadResult.Page(data = data, prevKey = null, nextKey = data.lastOrNull()?.jobId)
}
}
I was getting the same error and this is what worked for me. In the JodSliderDataSource class, toLoadResult method, set the nextKey parameter value by getting the page number from the response data and adding one.
private fun toLoadResult(
data: ArrayList<JobDetail>
): LoadResult<Int, JobDetail> {
return LoadResult.Page(
data = data,
prevKey = null,
nextKey = data.lastOrNull()?.jobId + 1 // Add one to the page number here.
)
}

Resources