I have a question related to recyclerview and I would like to know how can I recognize what element is selected from a recyclerview whose items has a checkbox each one.
I am using clean architecture and one of my layouts has an spinner where I can select a city and i make an http post request and the response are all the offices in this city. I take all this offices and insert in my layout but all this items has a check box each one and I have to recognize which office I selected and I only can choose one of them.
How can I recognize which item is selected and how can I make to force user to only select one of them?
You can use simple logic for that,
Generally i used to store Recyclerview index for that,we can use SetChecked listener on checkbox in recyclerview adapter, when you check any checkbox, store position value in any variable and after that you can get value from list or array which is used to store all offices data of this particular index.
Related
I implemented the code for filtering and also saving the position of the selected element in the listview. After the user makes search in the listview via editText a new list is created. When the user clicks on the element the position of it is being saved in sharedpreferences. By reopening the app, this position is needed but not the position from the filtered listview. It should be the right position of the original one. I didnt find any way to do this in my code.
I would really appreciate any help.
Thank you.
Saving position is not a convenient way to achieve the solution. Rather you need to store any unique value of the list.
Suppose, you are showing a list of Person and filtering the list based on user search. So, every Person will have one id say, p_idwhich is unique. Try to save p_id in your SharedPreference.
Then after reopening the app check the p_id of original list and mark then check or uncheck.
Your element should have its position as a field. Initialize the position during initialization or addition to the list.
I'm trying to save data in Room database from RecyclerView. The RecyclerView items contain an EditText (for weight) and a TextView (for names selected from user's Contacts). I want each name and weight to be saved in one row of my database.
My RecyclerView items are like this :
I can save only the Names right now. But I don't know what to do for EditTexts... How can I save the data in each recyclerview item as a row in database?
You must iterate your list that populates the recyclerview .
First of all : In your adapter send the current position and edit text value in the Activity/Fragment where you hold the recyclerview . You can use an interface or an EventBus , after that create a list to store all the data there for the current position .
After that iterate your created list with both your name and edit text value .
Hope it helps .
I am using a ListView in combination with a HashList to show data from an SQL Database through JSON. Everything works fine. But now I want to have a special feature.
In the ListView are round about 400 Items. So if i click one of the item to view its content and switch via Backbutton back to the Listview, i want to have the viewed Item from the Listview being marked in another color so that the user sees directly which oh the items he already has clicked.
I painted an little grafik for you, for better understanding
On the left side, the original View. After i View the 1st,5th and 8th Item of the list, the list should look like at the right side.
Is this possible?
You can implement your own adapter, then have an boolean array to store the current status of every row, and in the getView function assign a different color to the background of the selected rows based on your boolean array.
For this you will need also to set an onClick event in your rows to open the previsualization view and there also check the boolean array as "visited".
For more info about how to make custom efficient adapters, please read this
To simply let the user choice more than one row you can use
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
in your list
I am showing list of all the applications installed on the phone with the help of the packagemanager in the listview and there is a checkbox along with every application name shown in the list. I want to save the name of the applications against who the user has selected the textbox. The saved items will be used to generate a list of favorite apps.
You can Save it in the your database while the user presses on the check box and delete if it is already present in your database if the user unchecks it.You can use listener and put your code of saving inside your listener.
You should add a listener to your checkboxes => OnCheckedChangeListener
You will then be notified each time the state of the checkbox change, if the checkbox is checked, you can add the element to a list, if it is unchecked, you can remove the element from your list.
This requires you to have your own adapter thought.
Good luck and have a nice day
You need to make custom listview with checkboxes : here is one example for you.
You will need to have one Button below/above the Listview clicking on what you can take all the items and store it into your database : a complete Articale for that.
Now you can fetch the data from database anytime to show those favourite apps.
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.