I'm starting in the Kotlin/Java for android.
I read an external database and refresh the local database from the info I read.
I display my database information in a ListView without worries but I would have liked to refresh it with the click of a button. I correctly retrieve the external database and update it correctly but the ListView doesn't refresh and crashes if I click on it. However if I change screen and come back it works correctly and is refreshed.
Could someone please tell me how to properly update the ListView?
I was unable to get "notifydatasetchanged ()" to work.
Thanks for help
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
private lateinit var accesLocal : AccesLocal
@SuppressLint("UseRequireInsteadOfGet")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home,container,false)
accesLocal = AccesLocal(getActivity()?.getApplicationContext())
val ButtonRefresh: Button= root.findViewById(R.id.Refresh_list)
val ButtonNumber: Button= root.findViewById(R.id.BoutonNombre)
val textView: TextView = root.findViewById(R.id.text_home)
val ListeQuestion:ListView = root.findViewById(R.id.ListeQuestion)
if(accesLocal.number==0) {accesLocal.ajout(Question("Qu'est ce que cette app ?", 1))}
homeViewModel.text.observe(viewLifecycleOwner, Observer {textView.text = it})
ListeQuestion.setBackgroundColor(Color.GRAY)
ListeQuestion.adapter = getActivity()?.baseContext?.let { Adaptateur -> MyCustomAdapter(Adaptateur) }
ButtonRefresh.setOnClickListener {
view ->Snackbar.make(view,"Lecture base de donnée, wait", Snackbar.LENGTH_LONG).setAction("Action", null).show()
accesLocal= AccesLocal(getActivity()?.getApplicationContext())
val registrationForm1 : JSONObject = JSONObject()
try {registrationForm1.put("subject", "lire_tous");}
catch (e: JSONException) {e.printStackTrace();}
val body: RequestBody = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
registrationForm1.toString()
);
RequestSynchro(root, "http://ns328061.ip-37-187-112.eu:5000", body); //<-Refresh my local database
ListeQuestion.adapter = activity?.let { MyCustomAdapter(it.applicationContext) } //Dont works for refresh my list view
}
ButtonNumber.setOnClickListener {
val registrationForm1 : JSONObject = JSONObject()
try {registrationForm1.put("subject", "nombre");}
catch (e: JSONException) {e.printStackTrace();}
val body: RequestBody = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
registrationForm1.toString()
);
view?.let { it1 -> Snackbar.make(it1, "Post creation body, wait", Snackbar.LENGTH_LONG).setAction(
"Action",
null
).show() }
RequestNumber(root, "http://ns328061.ip-37-187-112.eu:5000", body);
}
return root
}
My adapter :
private class MyCustomAdapter(context: Context): BaseAdapter() {
private val mContext: Context
private lateinit var accesLocal : AccesLocal
init {
this.mContext = context
}
override fun getCount(): Int {
accesLocal = AccesLocal(mContext)
return accesLocal.number
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val textView = TextView(mContext)
accesLocal = AccesLocal(mContext)
textView.text = "Question numero "+(position+1)+"->\""+accesLocal.recupNumero(position + 1)+"\""
return textView
}
}
I would suggest using RecyclerView instead of ListView. Also, you can create a method in your adapter which would take the list of data and set it and use notifydatasetchanged there.