I have an Activity with a ListView and an adapter. Each row in my ListView contains one EditText and one CheckBox. I have two problems with this ListView:
first: what is the best way to save checked checkboxes after changing device orientation or pressing home button?
second: I implemented functionality to delete selected rows. When I click on checkbox and press a "delete button" the I call notifyDataSetChanged on my Adapter and everything is ok but if selected row isn't last row on my list then after all operations first field after deleted field becomes checked. Why?
The listview is recycling the views all the time. That means that the checkbox in the field after the deleted field is the SAME checkbox as was in the deleted field. That is why it becomes checked.
The solution is for the checked state to always be stored outside the view, in the activity. The easiest way is probably a Map of some key to a Boolean.
Try this..
Create Model fro list item and store checked and unchecked value to one variable and also you have to use getTag() and setTag() method.
Related
I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.
I have an activity which has, search view and recycler view. The recycler items contains checkbox, Assume there are 5 items(A,B,C,D,E) in recycler view . In normal, when I click checkbox on A/B/C/D/E,it works fine, when I search C on search view, the filter is done and it shows result of C alone, when I check C and close the search, the check is reassigned to A as it is in position 0 and C remains unchecked. I need to check based on user Id. How to achieve this?
For that purpose you need to validate data in proper manner.You just store the id of checkbox item when user is selecting to it.and after that you validate the checkbox data in onBindView() method of recycle adapter. I hope its help you.
For questions without code references, one can expect answers in the same manner, so first, try putting some code as well in the question.
In a nutshell, you need to save the selected state of the checkbox in your data model so that when the list is reloaded, you can get that selected state of checkbox and assign it to isChecked property, this can be done in onBindViewHolder. Hope this helps.
I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}
i have an app that has a listview. each view inside of the listview has custom xml and has buttons checkboxes and textviews. i have to get the previous saved settings for the checkboxes from an sqlite database and set them. and if the user changes the checkboxes or radio buttons i have to save those settings in the database.
tried several different ways to do this and most resulted in error and would not display the correct setting for checkboxes when coming back to the page.
what finally worked is the design shown below. where i put any database calls as close as possible to the actual view. however it looks like if there is many rows in the listview it would make many calls to the database and it could get slow.
is my design correct or is there a better way
psudocode
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
}
end arrayadapter
Your approach is correct and your concern is warranted. I can suggest you the following method, but its performance will have to be tested:
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
----> Create two ArrayLists: ArrayList<Boolean> for CheckBoxes
----> and ArrayList<Integer> for RadioButtons.
----> Fill these lists using the values from the databse
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
----> Here, update the lists, not the database.
----> Execute a AsyncTask to update the database whenever the lists change.
}
end arrayadapter
Another think you can do is to load data from database in Activity.onStart() and save it back in Activity onPause(). All other time use ArrayList to track changes in UI.
In that case you will have this heavy db operations much rarely during the Activity life cycle.
I'm using the cool feature of the ListView to show a checkbox next to the item in the ListView.
I bind my list to an array of strings.
The onClick and onSelectedItem listeners get called fine, in this way I know the index of the "string" checked (or unchecked).
I'm storing all the checked strings into preferences (as a comma-concatenated-string), and everytime the activity becomes visible I would like to set the checked items back in the listview.
Is there a way of doing it? or the CHOICE_MODE_MULTIPLE doesn't allow to set the checked items?
note: I'm not using a custom view, since what I want to display is just a string and a checkbox. I've tried setSelection(index) but it should set the onlyone selected (highlighted) row.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
m_playlists_list.setAdapter(adapter);
m_playlists_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Use the setItemChecked method of ListView
Sets the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.