简体   繁体   English

Android,如何从数据库更新列表视图中的选定项目?

[英]Android, How do i Update the selected item in my listview from my database?

Here's my code so far, but the application crashes when I press the update button. 到目前为止,这是我的代码,但是当我按下更新按钮时,应用程序崩溃了。

I want to update a selected item in my list, I have already created the update activity that will allow me to load the values on my database but I can't figure out how to load the value of selected item in list. 我想更新列表中的选定项目,我已经创建了更新活动,该活动将允许我将值加载到数据库中,但是我不知道如何加载列表中选定项的值。

{

        ArrayAdapter<String> ard=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_single_choice,list);
        lv.setAdapter(ard);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

          btnupdate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                SparseBooleanArray sba = lv.getCheckedItemPositions();
                Intent intent = new Intent(HomeworkInfo.this, UpdateHomework.class);
                startActivity(intent);
                finish();

            }



    });
}

When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. 构造ArrayAdapter时,它保留传入的List的引用。如果要传入的是Activity成员的List,并且以后再更改该Activity成员,则ArrayAdapter仍然持有对List的引用。原始清单。 The Adapter does not know you changed the List in the Activity. 适配器不知道您更改了活动中的列表。

Your choices are: 您的选择是:

  1. Use the functions of the ArrayAdapter to modify the underlying List (add(), insert(), remove(), clear(), etc.) 使用ArrayAdapter的功能来修改基础List(add(),insert(),remove(),clear()等)
  2. Re-create the ArrayAdapter with the new List data. 用新的List数据重新创建ArrayAdapter。 (Uses a lot of resources and garbage collection.) (使用大量资源和垃圾回收。)
  3. Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure. 创建从BaseAdapter和ListAdapter派生的您自己的类,该类允许更改基础List数据结构。
  4. Use the notifyDataSetChanged() every time the list is updated. 每次更新列表时,请使用notifyDataSetChanged()。 To call it on the UI-Thread, use the runOnUiThread() of Activity. 要在UI线程上调用它,请使用Activity的runOnUiThread()。 Then, notifyDataSetChanged() will work. 然后,notifyDataSetChanged()将起作用。

I hope this helps you .. Happy coding !! 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问Firebase数据库中的子项,并根据所选的listview项在活动中显示它们 - How do I access the children in my firebase database and display them in the activity according to the listview item selected 如何从数据库的listView中的选定项中获取数据并在alertdialog上查看? - how I can get the data from the selected item in the listView from my database and view it on alertdialog? 选择微调器中的项目时,如何更改列表视图的内容? - How do I change the content of my listview when an item in my spinner is selected? 如何在祝酒消息中成功显示所选的列表视图项? - How do I successfully show my selected listview item in a toast message? 如何在列表视图中显示数据库中的数据? - How do i display data from my database in a listview? 如何在Android ListView中设置所选项目的样式? - How do I style selected item in Android ListView? 如何从Database和Listview中删除所选项目 - How can i delete selected item from Database and Listview both 如何将选定项目的ID从ListView传递到Android中的AlertDialog? - How do I pass the selected item's ID from a ListView to an AlertDialog in Android? 如何从数据库中删除选定的列表视图项 - how to delete selected listview item from database 从数据库中删除项目后如何刷新列表视图 - How to refresh my listview after deleting an item from database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM