0

I am developing an app for attendance. So I want to retrieve my roll no. and name data from my database and display it in a table layout and add 2 radio buttons (present and absent) with each record, but I am unable to do so.

This is my code:

package com.example.attendance;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Take_attendance1 extends AppCompatActivity {

    TextView st_roll,st_name;
    Databases db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_attendance1);

        db = new Databases(this);
        st_roll = (TextView)findViewById(R.id.st_roll1);
        st_name = (TextView)findViewById(R.id.st_name1);
        String course = getIntent().getStringExtra("coursesel");
        String sem = getIntent().getStringExtra("semsel");

        db = new Databases(this);
        StringBuilder stringBuilder[] = db.getStudentData(course,sem);

        st_roll.setText(stringBuilder[1]); //for roll no
        st_name.setText(stringBuilder[2]); //for name
    }
}

Here is the snapshot of my app:

enter image description here

Now I want radio buttons for all my records, but it is showing only for the first one.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68

2 Answers2

0

You could try using a RecyclerView or a ListView . It would be less time consuming than setting textviews and radiobuttons for individual students as you only have to design the layout for one student Viewholder and then just pass the data through the adapter.

For help with List View Refer: ListView Documentation

For help with RecyclerView try these two links:

ReyclerView Documentation

RecyclerView Guide

I recommend you use a ListView if the data isn't much. However if you do choose to use a RecyclerView this might be useful.

CheckBox in RecyclerView keeps on checking different items

If you are having a hardtime choosing between the two read this question.

RecyclerView vs. ListView

EDIT

So in the comments you asked me how you could differentiate between the radio buttons as you will have only one id.

Set an OnCheckedStateChanged Listener to the radio group that you have. The you could create a Hashmap/List/Array or you could even update your base directly. Here is an example.

public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {  
     Boolean attendance;   
     //set other data from database here.
     holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
      selected = holder.findViewById(checkedId)
      //checkedid is id of selected button.

    }
});
if(selected.getText().toString.equals(P))
   //update database
   //You will have the roll.no of the current student which I'm assuming is your primary key 


}
Aditya Kurkure
  • 422
  • 5
  • 19
  • If I use listview, how would I differenciate between different radio buttons as I will have only one id for the radio button so how can I take values for different student's attendance. – ashu rawat May 17 '20 at 05:26
  • @ashurawat I have edited my answer you can check it out now – Aditya Kurkure May 18 '20 at 06:32
0

Try I using the loops But better option would be using a listview or a upgraded version of it i.e recycler view