Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to use radio buttons in a form within a database in Android Studio? I have attempted the coding...but it is not correct. //content_screen3.xml <

How to use radio buttons in a form within a database in Android Studio? I have attempted the coding...but it is not correct.

//content_screen3.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingTop="70dp"  > <TextView  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/response2" /> <TextView  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/taskrec" /> <TableLayout  android:id="@+id/add_table"  android:layout_width="match_parent"  android:layout_height="379dp"  android:paddingTop="150dp"> <TableRow> <TextView  android:layout_marginLeft="25dp"  android:padding="3dip"  android:text="Task Name:" /> <EditText  android:id="@+id/tn"  android:layout_width="190dp"  android:layout_height="wrap_content" /> TableRow> <TableRow> <TextView  android:layout_marginLeft="25dp"  android:padding="3dip"  android:layout_marginTop="20dp"  android:text="Location:" /> <EditText  android:id="@+id/loc"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="20dp"  android:minWidth="150dip" /> TableRow> <TableRow> <TextView  android:layout_marginLeft="20dp"  android:padding="7dip"  android:layout_marginTop="20dp"  android:text="Status:" /> <RadioGroup  android:id="@+id/radioStatus"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="20dp"  android:orientation="horizontal"> <RadioButton  android:id="@+id/completed"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:scaleX="0.9"  android:scaleY="0.9"  android:text="Completed" /> <RadioButton  android:id="@+id/notcompleted"  android:layout_width="wrap_content"  android:layout_height="match_parent"  android:scaleX="0.9"  android:scaleY="0.9"  android:text="Not Completed"  android:checked="true"/> RadioGroup> TableRow> <Button  android:id="@+id/add_button2"  android:layout_width="207dp"  android:layout_height="wrap_content"  android:layout_marginLeft="80dp"  android:layout_marginRight="86dp"  android:layout_marginTop="14dp"  android:padding="6dip"  android:text="Add Task" /> TableLayout> LinearLayout>

//Screen3.java

package com.user.myproject; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.EditText; import android.widget.TableLayout; import android.widget.Button; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import java.util.ArrayList; public class Screen3 extends AppCompatActivity { private DatabaseManager mydManager; private TextView response2; private TextView taskRec; private EditText tn, loc; private RadioGroup stat; private RadioButton statselect; private Button addButton2; private TableLayout addLayout; private boolean recInserted; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle(null); mydManager = new DatabaseManager(Screen3.this); response2 = (TextView)findViewById(R.id.response2); taskRec = (TextView)findViewById(R.id.taskrec); addLayout = (TableLayout)findViewById(R.id.add_table); addLayout.setVisibility(View.GONE); response2.setText("Press MENU button to display menu"); addButton2 = (Button) findViewById(R.id.add_button2); addButton2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { tn = (EditText)findViewById(R.id.tn); loc = (EditText)findViewById(R.id.loc); stat = (RadioGroup)findViewById(R.id.radioStatus); int selectedId = stat.getCheckedRadioButtonId(); statselect = (RadioButton) findViewById(R.id.completed); statselect = (RadioButton) findViewById(R.id.notcompleted); String status = completed.getText(); String status = notcompleted.getText(); recInserted = mydManager.addRow2(tn.getText().toString(),loc.getText().toString(), status); addLayout.setVisibility(View.GONE); if (recInserted) { response2.setText("The row in the tasks table is inserted"); } else { response2.setText("Sorry, errors when inserting to DB"); } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // imm.hideSoftInputFromWindow(price.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  mydManager.close(); tn.setText(""); loc.setText(""); taskRec.setText(""); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.menu_screen3, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will  // automatically handle clicks on the Home/Up button, so long  // as you specify a parent activity in AndroidManifest.xml.  int id = item.getItemId(); switch (item.getItemId()) { case R.id.addtask: insertRec(); break; case R.id.edittask: removeRecs(); break; case R.id.viewtask: showRec2(); break; } return super.onOptionsItemSelected(item); } public boolean insertRec() { addLayout.setVisibility(View.VISIBLE); response2.setText("Enter information of the new task"); taskRec.setVisibility(View.GONE); return true; } public boolean showRec2() { addLayout.setVisibility(View.GONE); taskRec.setVisibility(View.VISIBLE); mydManager.openReadable(); ArrayList tableContent = mydManager.retrieveRows2(); response2.setText("The rows in the tasks table are: "); String info = ""; for (int i = 0; i < tableContent.size(); i++) { info += tableContent.get(i) + " "; } taskRec.setText(info); System.out.println(info); return true; } public boolean removeRecs() { mydManager.clearRecords2(); response2.setText("All Records are removed"); taskRec.setText(""); return true; } } //DatabaseManager.java 
package com.user.myproject; import android.database.sqlite.SQLiteDatabase; import android.content.Context; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import java.util.ArrayList; public class DatabaseManager { public static final String DB_NAME = "student"; public static final String DB_TABLE = "information"; public static final String DB_TABLE2 = "information2"; public static final int DB_VERSION = 1; private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + DB_TABLE + " (studentId INTEGER, first_name TEXT, last_name TEXT, gender TEXT, course_study TEXT, age INTEGER, address TEXT);"; private static final String CREATE_TABLE2 = "CREATE TABLE IF NOT EXISTS " + DB_TABLE2 + " (task_name TEXT, location INTEGER);"; private SQLHelper helper; private SQLiteDatabase db; private Context context; public DatabaseManager(Context c) { this.context = c; helper = new SQLHelper(c); this.db = helper.getWritableDatabase(); } public DatabaseManager openReadable() throws android.database.SQLException { helper = new SQLHelper(context); db = helper.getReadableDatabase(); return this; } public void close() { helper.close(); } public boolean addRow(Integer c, String fn, String ln, String ge, String cs, Integer ag, String ad) { synchronized (this.db) { ContentValues newStudent = new ContentValues(); newStudent.put("studentId", c); newStudent.put("first_name", fn); newStudent.put("last_name", ln); newStudent.put("gender", ge); newStudent.put("course_study", cs); newStudent.put("age", ag); newStudent.put("address", ad); try { db.insertOrThrow(DB_TABLE, null, newStudent); } catch (Exception e) { Log.e("Error in inserting rows", e.toString()); e.printStackTrace(); return false; } //db.close();  return true; } } public boolean addRow2(String tn, String lo) { synchronized (this.db) { ContentValues newTask = new ContentValues(); newTask.put("task_name", tn); newTask.put("location", lo); try { db.insertOrThrow(DB_TABLE2, null, newTask); } catch (Exception e) { Log.e("Error in inserting rows", e.toString()); e.printStackTrace(); return false; } //db.close();  return true; } } public ArrayList retrieveRows() { ArrayList studentRows = new ArrayList(); String[] columns = new String[]{"studentId", "first_name", "last_name", "gender", "course_study", "age", "address"}; Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { studentRows.add(Integer.toString(cursor.getInt(0)) + ", " + cursor.getString(1) + ", " + cursor.getString(2) + ", " + cursor.getString(3) + ", " + cursor.getString(4) + ", " + Integer.toString(cursor.getInt(5)) + ", " + cursor.getString(6)); cursor.moveToNext(); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return studentRows; } public ArrayList retrieveRows2() { ArrayList taskRows = new ArrayList(); String[] columns = new String[]{"task_name", "location"}; Cursor cursor = db.query(DB_TABLE2, columns, null, null, null, null, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { taskRows.add(cursor.getString(0) + ", " + cursor.getString(1)); cursor.moveToNext(); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return taskRows; } public void clearRecords() { db = helper.getWritableDatabase(); db.delete(DB_TABLE, null, null); } public void clearRecords2() { db = helper.getWritableDatabase(); db.delete(DB_TABLE2, null, null); } public class SQLHelper extends SQLiteOpenHelper { public SQLHelper(Context c) { super(c, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); db.execSQL(CREATE_TABLE2); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w("Students table", "Upgrading database i.e. dropping table and re-creating it"); db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE2); onCreate(db); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions