How to store radio button value from recyclerview in sqlite - android

I am using recyclerview in my app to display employee .I have radio buttons inside the cards which i need to select (i.e Present and absent) and store the date where only if employee is absent
My database to store the value would be like
Attendance Table
A_id (p.k)
Emp_id (f.k)
Dates (date value)
So i don't know how to get values of radio button from card and enter in database for that selected employee id (lets assume i have emp_id retrieve from my databases so that i can pass it while insertion of attendance data)
I have specified a mark image button above the recyclerview so that it can take the values of selected radio button(which is absent) and stored it in database .
Can anyone help me with how to get selected radio button from card view (there are many entries i.e many cards ) using onclicklistener of image
Thanks in Advance .

In your recyclerviews adapter,
in the view holder class, set on check listener for the checkbox.
inside the listener, update the dataset you are passing to the recycler view adapter.
So you now have the dataset with details about which boxes have been checked.
now write a public method,say getUpdatedData(), inside the adapter class to return the data to your main activity.
Now in your imageviews onclick listener, call this method to get the data set. Iterate through the data and you will get which buttons are checked.
Hope you get what I'm trying to say
I could show you some code, if you had posted your code.

Related

Android sqlite pagination in TextView

I am using Android studio with SQLite database is built-in, I am fetching some text from a table of 2 columns into TextView.
I want to use next and previous button
When the Activity starts, it loads the first record of a table in TextView but when I click on next button it should fetch next row of the table in TextView and when I click previous it should fetch the previous row of the table.
I am new to Android I searched about that but unfortunately found nothing.
try this. it could help you.
when a user enters in Activity fetch all data from Database and store that data in ArrayList at particular index . On click of Next Button get data from ArrayList according to it's index.

How can I retrieve selection count of each item under ListView in android?

I have created an android app that contains a ListView with some items in it. I want to get the details of each item with the count of it being selected.
Ex : 1st item is selected twice, 2nd item is selected 5times etc.
Forgive me if I am misunderstanding your question but, its simple just update the list view on every selection using notifyDatasetChanged() method of your list view adapter
Use a data structure in Adapter. HashMap is a good idea, where key is position and and value is count. In adapter use getter, to get HashMap in actvity (via adapter.getMap();) .
call map.put(position,count) in onclick of view.
HashMap ensures only one key (position) is maintained with latest count (since HashMap maintains unique keys and overrides previous one if the key already exists)
Let me know if you require the code as well.

Spinner that returns multiple value on selected?

is it possible for a Spinner to return multiple values or class object on selected?
For example I have a Spinner of Laptop models. When selected I want it to return LaptopSpecs object that contains size, weight, processor, etc. Then use it to display the information in the view below it.
Thanks
Sorry, there is no multi-select Spinner. You are welcome to use a multi-select list AlertDialog to allow the user to make their selection(s), but you will need to decide for yourself how you want to render those selection(s) when the dialog is not on the screen.
It depends on how you are populating your spinner.
If you are pulling the data from a database in a cursor, what you are trying to do is easy.
As a matter of fact, using a database, there's a couple ways you can do it:
1) You simply pull all the necessary data you need to create the object into your cursor (kinda heavy load on the front end), and when a selection is made (fromthe single bit of data displayed in the spinner), you use the cursor position reference in the onItemSlected method to pull the related data from the cursor and pack it into your object.
2) You pull only the piece of data to display in the spinner and when a selection is made, use the database row id in the onItemSelected method to fetch the rest of the data for your object from the database.

How to display dynamic data in ListView with ordering

What I want to do:
Display a list of items
The actual data is coming from an external source and can change
Each item can have several "columns". E.g. "type", "content", "date"
The list should e.g. be sortable by "type" and "date".
I thought I might be able to get the desired functionality using a ListView (but maybe also this is a very bad choice?). I have read some stuff about them, but still have some questions. I know that the ListView Displays data which is managed by an Adapter. If I have an ArrayAdapter, does the Listview always display the items in the order of the underlying Array in the Adapter? So to implement sorting, I would have to somehow change the Array in the Adapter? I have read, that you can use SimpleAdapter to have several properties in a row, but the data SimpleAdapter uses seems to be static. How can I achieve that?
Thanks in advance!
Yes, ListView will represent depending on how your ArrayList or Vector([]) is sorted. Now, if you want to have several properties for each row, I would recommend that you create an Object of your choice, for instance: class Person which takes name, age, address etc etc.
Then create a list which takes Person objects. Then in your getView of your ArrayAdapter that you will override, you can call Person person = getItem(position); and now you have a hold of that object in that specific row (position), and can do whatever you want with that Person object.
Create your own adapter, and in getView() return view that matches your position. For instance if you got data like this:
id name
0 ccc
1 bbb
2 aaa
then when you sort by id and listview wants row at position 1, you return 1 bbb. But if you sort by name ascending, and list wants row #2, then you return 0 ccc. Of course you need to maintain the "mapping", but that's rather trivial.

Android: How to get the selected item in listview using cursor?

I am very new to Android.
Can anybody tell me how can I get the selected item from the ListView when the data is coming from a Cursor
Thanks.
If you created a ListActivity (which has a ListView in it), the onListItemClick()-method is called every time a entry in the list is clicked. This method has a parameter long id which contains the ID of your selected Item.
The idea behind this is, that every entry in your SQLite Database has a unique ID (using auto_increment). If you set your ListActivity up with a SimpleCursorAdapter, you'll need to have a column named _id (if you have a ID-column with another name, use the AS-function). This column is automatically used to determine which ID the clicked entry has.
So lets say one of your entry's has the ID 12 and this ID is in the column _id. If you select this entry from your ListView, the onListItemClick()-method's id-parameter will contain the value 12.
This is the easiest way if you're using a Database for your content (like in a Notebook).
You set an OnItemClickListener for the ListView using the setOnItemClickListener method.
Within that method you have the selected position and you can call your adapter's getItem method for that position.
You should probably override getItem in your adapter to return a properly constructed object from your domain.

Categories

Resources