Thurzton Muyot
Thurzton Muyot

Reputation: 1

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();

            }



    });
}

Upvotes: 0

Views: 222

Answers (1)

Trideep
Trideep

Reputation: 396

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. 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.)
  2. Re-create the ArrayAdapter with the new List data. (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.
  4. Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity. Then, notifyDataSetChanged() will work.

I hope this helps you .. Happy coding !!

Upvotes: 2

Related Questions