MainActivity - Java, Students Info
MainActivity - Java, Students Info
MainActivity - Java, Students Info
Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateStudentList();
}
if (rollNo.isEmpty() || name.isEmpty()) {
Toast.makeText(this, "Please enter roll no. and name",
Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.STUDENT_COLUMN_ROLL_NO,
Integer.parseInt(rollNo));
values.put(DatabaseHelper.STUDENT_COLUMN_NAME, name);
long result = db.insert(DatabaseHelper.STUDENT_TABLE_NAME, null, values);
if (result != -1) {
Toast.makeText(this, "Student added successfully",
Toast.LENGTH_SHORT).show();
populateStudentList();
} else {
Toast.makeText(this, "Failed to add student",
Toast.LENGTH_SHORT).show();
}
}
while (cursor.moveToNext()) {
int rollNo =
cursor.getInt(cursor.getColumnIndex(DatabaseHelper.STUDENT_COLUMN_ROLL_NO));
String name =
cursor.getString(cursor.getColumnIndex(DatabaseHelper.STUDENT_COLUMN_NAME));
students.add(new Student(rollNo, name));
}
adapter.notifyDataSetChanged();
}
}