I am parsing Json data in an Arraylist but data added two times i removed temp.addAll(value) line but Nothing happened.please help me and answers should be in Kotlin if Possible.
here is the code
Pojo classes: class Restaurant_info (
val id :String?=null,
val name :String?=null,
val rating : String?=null,
val cost_for_one: String?=null,
val image_url: String?=null
)
home (fragment): lateinit var swipe: SwipeRefreshLayout
lateinit var value :ArrayList
lateinit var temp :ArrayList
lateinit var recyclerView : RecyclerView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_dashboard, container, false)
temp=ArrayList()
value=ArrayList()
setHasOptionsMenu(true)
swipe = view.findViewById(R.id.swipe)
var relativeLayout = view.findViewById(R.id.progresslaoout)
recyclerView = view.findViewById(R.id.recycler)
var LayoutManager = LinearLayoutManager(activity)
gettingData()
swipe.setOnRefreshListener {
gettingData()
}
relativeLayout.visibility = View.VISIBLE
val que = Volley.newRequestQueue(activity as Context)
var url = “http://13.235.250.119/v2/restaurants/fetch_result/”
var jsonObjectRequest =
object : JsonObjectRequest(Method.GET, url, null, Response.Listener {
try {
relativeLayout.visibility = View.GONE
var list =it.getJSONObject(“data”)
for(a in 0 until list.length()){
var data = list.getJSONArray(“data”)
for (i in 0 until data.length()) //compiler yhi bolta h ki tum null aray ki length le rhe ho
{
var jsonobject = data.getJSONObject(i)
var values= Restaurant_info (
jsonobject.getString(“id”),
jsonobject.getString(“name”),
jsonobject.getString(“rating”),
jsonobject.getString(“cost_for_one”),
jsonobject.getString(“image_url”)
)
value.add(values)
//
}
}
temp.addAll(value)
recyclerView.adapter = adapter1(activity as Context, temp)
recyclerView.layoutManager = LayoutManager
} catch (e: JSONException) {
makeText(context, “some error occured catch”, Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
makeText(context, “some error occured sorry”, Toast.LENGTH_SHORT).show()
}) {
override fun getHeaders(): MutableMap
val header = HashMap
header.put(“value-type”, “application/json”)
header.put(“token”, “9bf53418365f1”)
return header
}
}
que.add(jsonObjectRequest)
return view
}
fun gettingData() {
if (connection().ConnectionChecking(activity as Context)) {
} else {
var build = AlertDialog.Builder(activity as Context)
build.setTitle(“Not Connected”)
build.setMessage(“please check your internet Connection”)
build.setPositiveButton(“try again”) { text, listner ->
startActivityForResult(Intent(Settings.ACTION_WIRELESS_SETTINGS), 0)
}
build.setNegativeButton(“cancel”) { text, listner ->
}
build.create()
build.show()
}
swipe.isRefreshing = false
}
@SuppressLint(“NewApi”)
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.icon,menu)
var item=menu.findItem(R.id.search)
var search=item?.actionView as SearchView
search.onActionViewExpanded()
search.focusable
search.requestFocus()
search.requestFocusFromTouch()
search.queryHint=”enter food name”
search.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(p0: String?): Boolean {
return true
}
@SuppressLint(“NotifyDataSetChanged”)
override fun onQueryTextChange(p0: String?): Boolean {
temp.clear()
var text= p0?.lowercase(Locale.getDefault())
if (text!!.isNotEmpty()) {
value.forEach {
//if(it.lowercase(Locale.getDefault()).contains(text))
if (it.name?.lowercase(Locale.getDefault())?.contains(text) == true) {
temp.add(it)
}
}
recyclerView.adapter?.notifyDataSetChanged()
}
else {
temp.addAll(value)
recyclerView.adapter?.notifyDataSetChanged()
}
return false
}
})
super.onCreateOptionsMenu(menu, inflater)
}
}
adapter class: class adapter1(var context : Context, var ada : ArrayList
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewholderclass {
var view= LayoutInflater.from(parent.context).inflate(R.layout.adapter,parent,false)
return viewholderclass(view)
}
override fun onBindViewHolder(holder: viewholderclass, position: Int) {
var id=ada[position]
holder.name.text=ada[position].name
holder.rating.text=ada[position].rating
holder.cost.text=ada[position].cost_for_one
Picasso.get().load(id.image_url).into(holder.image)
}
override fun getItemCount() :Int{
return ada.size /* returning the size */
}
class viewholderclass(view: View) : RecyclerView.ViewHolder(view) {
var name=view.findViewById(R.id.name)
var rating=view.findViewById(R.id.rating)
var cost=view.findViewById(R.id.price)
var image=view.findViewById(R.id.image)
var layout: RelativeLayout =view.findViewById(R.id.adapter)
}
}
json object: “data”: {
“success”: true,
“data”:[[
{
“id”: “1”,
“name”: “Pind Tadka”,
“rating”: “4.1”,
“cost_for_one”: “280”,
“image_url”: “https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940”
},
{
“id”: “2”,
“name”: “Garbar Burgers”,
“rating”: “4.6”,
“cost_for_one”: “200”,
“image_url”: “https://images.pexels.com/photos/1639565/pexels-photo-1639565.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940”
},
and its going so on till id 18