0% found this document useful (0 votes)
19 views2 pages

MainActivity - Java, Students Info

The document describes code for an Android application that allows adding students to a database table and displaying them in a list view. It imports necessary classes, defines fields for database helper, list view and adapter, adds a click listener to an add button that calls a method to insert a new student, and populates the list view from the database on creation and after adding.

Uploaded by

Soham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

MainActivity - Java, Students Info

The document describes code for an Android application that allows adding students to a database table and displaying them in a list view. It imports necessary classes, defines fields for database helper, list view and adapter, adds a click listener to an add button that calls a method to insert a new student, and populates the list view from the database on creation and after adding.

Uploaded by

Soham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import android.os.

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;

public class MainActivity extends AppCompatActivity {


private DatabaseHelper dbHelper;
private ListView listView;
private ArrayAdapter<Student> adapter;
private ArrayList<Student> students;

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

dbHelper = new DatabaseHelper(this);


listView = findViewById(R.id.listView);
students = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
students);
listView.setAdapter(adapter);

Button addButton = findViewById(R.id.addButton);


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addStudent();
}
});

populateStudentList();
}

private void addStudent() {


EditText rollNoEditText = findViewById(R.id.rollNoEditText);
EditText nameEditText = findViewById(R.id.nameEditText);

String rollNo = rollNoEditText.getText().toString();


String name = nameEditText.getText().toString();

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();
}
}

private void populateStudentList() {


students.clear();
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(DatabaseHelper.STUDENT_TABLE_NAME, null, null,
null, null, null, null);

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();
}
}

You might also like