【问题标题】:How refresh ListView?ListView如何刷新?
【发布时间】:2021-05-04 21:10:53
【问题描述】:

我从 Kotlin/Java for android 开始。

我读取了一个外部数据库并根据我读取的信息刷新本地数据库。

我毫无顾虑地在 ListView 中显示我的数据库信息,但我希望通过单击按钮来刷新它。我正确地检索了外部数据库并正确更新了它,但是如果我单击它,ListView 不会刷新并崩溃。但是,如果我更改屏幕并返回,它会正常工作并被刷新。

有人可以告诉我如何正确更新 ListView 吗?

我无法让“notifydatasetchanged()”工作。

感谢您的帮助

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
}

我的适配器:

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

【问题讨论】:

  • how to properly update the ListView 不要使用listview,使用recyclerview

标签: android kotlin listview android-listview refresh


【解决方案1】:

我建议使用 RecyclerView 而不是 ListView。此外,您可以在适配器中创建一个方法,该方法将获取数据列表并设置它并在那里使用 notifydatasetchanged。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多