RadioButton не отображается. Согласно моему коду, должен появиться переключатель с именем CSE, но он не виден, потому что во время создания базы данных я добавляю один курс с подробным названием CSE в DatabaseHelper. java. Должен появиться хотя бы один переключатель.
Я не понимаю, в чем проблема.
CreateStudent. java
EditText firstnm,lastnm,email,contact;
Button add;
RadioGroup radioGroup;
DatabaseHelper db;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_student);
db=new DatabaseHelper(this);
Toolbar toolbar = findViewById(R.id.tool);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(CreateStudent.this,Home.class);
startActivity(intent);
}
});
firstnm=findViewById(R.id.teacherfirstnm);
lastnm=findViewById(R.id.teacherlastnm);
email=findViewById(R.id.teacheremail);
contact=findViewById(R.id.teachercontact);
add=findViewById(R.id.addstudent);
radioGroup=findViewById(R.id.radio);
ArrayList<String> coursenm = new ArrayList<String>(db.getCourseName());
if (coursenm!=null)
{
RadioButton[] rb=new RadioButton[coursenm.size()];
for (int i=0;i<rb.length;i++){
rb[i]=new RadioButton(this);
}
for (int i=0;i<coursenm.size();i++){
rb[i].setText(coursenm.get(i).toString());
rb[i].setId(Integer.parseInt(coursenm.get(i)));
radioGroup.addView(rb[i]);
}
}
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String first=firstnm.getText().toString().trim();
String last=lastnm.getText().toString().trim();
String emailid=email.getText().toString().trim();
String contactno=contact.getText().toString().trim();
int selectedRB=radioGroup.getCheckedRadioButtonId();
RadioButton selectedRButton=(RadioButton) findViewById(selectedRB);
final String selectRBText=selectedRButton.getText().toString();
if (first == null || last == null || emailid == null || contactno == null || selectRBText==null){
Toast.makeText(CreateStudent.this, "All Fields are Required", Toast.LENGTH_SHORT).show();
firstnm.setText("");
lastnm.setText("");
email.setText("");
contact.setText("");
radioGroup.clearCheck();
}
else {
Long val=db.addStudent(first,last,emailid,contactno,selectRBText);
if(val > 0) {
Toast.makeText(CreateStudent.this, " You Have Added Successfully ", Toast.LENGTH_LONG).show();
startActivity(new Intent(CreateStudent.this, Home.class));
}
else{
Toast.makeText(CreateStudent.this, " Error", Toast.LENGTH_LONG).show();
}
}
}
});
cratestudent. xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateStudent">
<include
android:id="@+id/tool"
layout="@layout/toolbar"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tool">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0F504848"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:textColorHint="#FE22B3E8">
<EditText
android:id="@+id/teacherfirstnm"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/textinputborder"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName"
android:shadowColor="#000" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#FE22B3E8"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<EditText
android:id="@+id/teacherlastnm"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/textinputborder"
android:hint="Last Name"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColorHint="#FE22B3E8">
<EditText
android:id="@+id/teachercontact"
android:background="@drawable/textinputborder"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:hint="Contact Name"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#FE22B3E8"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<EditText
android:id="@+id/teacheremail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:background="@drawable/textinputborder"
android:hint="Email Id"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="#1B1A1A"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Select Course : "
android:textSize="25sp"
android:textStyle="bold" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:id="@+id/radio"
android:background="@drawable/textinputborder"
android:layout_height="wrap_content">
</RadioGroup>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:id="@+id/addstudent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="Add Student"
android:layout_marginBottom="20dp"
android:background="#FE22B3E8"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
DatabaseHelper. java
public String current_email;
public String current_user;
public static final String DATABASE_NAME = "Attendence.db";
public static final String TABLE_NAME = "user";
public static final String USER_COL_1 = "ID";
public static final String USER_COL_2 = "fullname";
public static final String USER_COL_3 = "emailid";
public static final String USER_COL_4 = "username";
public static final String USER_COL_5 = "password";
public static final String USER_COL_6 = "account_type";
public static final String TABLE_NAME2 ="student";
public static final String STUDENT_COL1 = "ID";
public static final String STUDENT_COL2 = "firstname";
public static final String STUDENT_COL3 = "lastname";
public static final String STUDENT_COL4 = "contactno";
public static final String STUDENT_COL5 = "emailid";
public static final String STUDENT_COL6 = "course";
public static final String TABLE_NAME3 ="teacher";
public static final String TEACHER_COL1 = "ID";
public static final String TEACHER_COL2 = "firstname";
public static final String TEACHER_COL3 = "lastname";
public static final String TEACHER_COL4 = "contactno";
public static final String TEACHER_COL5 = "emailid";
public static final String TABLE_NAME4 ="course";
public static final String COURSE_COL1 = "ID";
public static final String COURSE_COL2 = "coursecode";
public static final String COURSE_COL3 = "coursename";
public static final String COURSE_COL4 = "teachername";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (id integer primary key autoincrement ,fullname text,emailid text,username text,password text , account_type text)");
db.execSQL("create table " + TABLE_NAME2 + " ( id integer primary key autoincrement , firstname text, lastname text, contactno integer, emailid text , course text ) ");
db.execSQL("create table " + TABLE_NAME3 + " ( id integer primary key autoincrement , firstname text, lastname text,contactno integer , emailid text ) ");
db.execSQL("create table " + TABLE_NAME4 + " (id integer primary key autoincrement , coursecode text, coursename text, teachername text ) ");
db.execSQL("insert into " + TABLE_NAME4 +"(coursecode , coursename , teachername ) values ('CSE','CSE','Pradeep')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME2);
onCreate(db);
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME3);
onCreate(db);
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME4);
onCreate(db);
}
public Boolean checkUser(String user, String pwd , String acc_type) {
String[] columns = {USER_COL_1};
SQLiteDatabase db = this.getReadableDatabase();
String selection = USER_COL_4 + "=?" + " and " + USER_COL_5 + "=?" + " and " + USER_COL_6 + "=?";
String[] selectionArgs = {user, pwd , acc_type};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
db.close();
if (count > 0) {
current_user = user;
return true;
}
else
return false;
}
public Long addUser(String full_nm, String email_id, String user, String pwd , String acc_type) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues c=new ContentValues();
c.put(USER_COL_2,full_nm);
c.put(USER_COL_3,email_id);
c.put(USER_COL_4,user);
c.put(USER_COL_5,pwd);
c.put(USER_COL_6,acc_type);
Long res=db.insert(TABLE_NAME,null,c);
db.close();
return res;
}
public String getCurrentUser()
{
return current_user;
}
public String getCurrentUserEmail()
{
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT " + USER_COL_3 +" FROM " + TABLE_NAME + " WHERE " + USER_COL_4 +" = '" + current_user +"'";
Cursor cursor=db.rawQuery(selectquery,null);
cursor.moveToFirst();
current_email=cursor.getString(cursor.getColumnIndex(USER_COL_3));
cursor.close();
return current_email;
}
public Long addStudent(String first, String last, String emailid, String contactno , String course) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues c=new ContentValues();
c.put(STUDENT_COL2,first);
c.put(STUDENT_COL3,last);
c.put(STUDENT_COL4,contactno);
c.put(STUDENT_COL5,emailid);
c.put(STUDENT_COL6,course);
Long res=db.insert(TABLE_NAME2,null,c);
db.close();
return res;
}
public Long addTeacher(String first, String last, String contactno, String emailid) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues c=new ContentValues();
c.put(TEACHER_COL2,first);
c.put(TEACHER_COL3,last);
c.put(TEACHER_COL4,contactno);
c.put(TEACHER_COL5,emailid);
Long res=db.insert(TABLE_NAME3,null,c);
db.close();
return res;
}
public String[] getTeacherfirstnm() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME3 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(TEACHER_COL2)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public String[] getTeacherlastnm() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME3 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(TEACHER_COL3)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public String[] getTeacherContact() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME3 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(TEACHER_COL4)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public String[] getStudentFirstName() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME2 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(STUDENT_COL2)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public String[] getStudentLastName() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME2 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(STUDENT_COL3)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public String[] getStudentCourse() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME2 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(STUDENT_COL6)));
i++;
res.moveToNext();
}
res.close();
return array;
}
public Long addCourse(String code, String name, String teachernm) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues c=new ContentValues();
c.put(COURSE_COL2,code);
c.put(COURSE_COL3,name);
c.put(COURSE_COL4,teachernm);
Long res=db.insert(TABLE_NAME4,null,c);
db.close();
return res;
}
public ArrayList<String> getCourseName() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME4 ;
Cursor res=db.rawQuery(selectquery,null);
ArrayList<String> array=new ArrayList<>();
if (res!=null && res.moveToFirst()) {
while (res.isAfterLast()) {
array.add(res.getString(res.getColumnIndex(COURSE_COL3)));
res.moveToNext();
}
res.close();
return array;
}
else {
return null;
}
}
public String[] getCourseCode() {
SQLiteDatabase db = this.getReadableDatabase();
String selectquery="SELECT * FROM " + TABLE_NAME4 ;
Cursor res=db.rawQuery(selectquery,null);
int count=res.getColumnCount();
String[] array = new String[count];
res.moveToFirst();
while (res.isAfterLast()){
int i=1;
array[i]=(res.getString(res.getColumnIndex(COURSE_COL2)));
i++;
res.moveToNext();
}
res.close();
return array;
}
}