Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with getting correct output in android studio. There are two screens where the first screen has a button called Add A Student. When

Need help with getting correct output in android studio. There are two screens where the first screen has a button called "Add A Student". When this button is clicked, the app is supposed to move onto the next screen showing a table layout. The first image is what I am trying to achieve and second image is what I am currently getting.

image text in transcribed

image text in transcribed

//content_main.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="60dp"> Button  android:id="@+id/button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginLeft="71dp"  android:layout_marginTop="110dp"  android:onClick="addRecord"  android:text="Add A Record"  /> Button  android:id="@+id/editstudentButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginLeft="200dp"  android:layout_marginRight="10dp"  android:layout_marginTop="-89dp"  android:padding="15dip"  android:text="Edit A Record" /> LinearLayout> 

//content_main2.xml

xml version="1.0" encoding="utf-8"?> LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_marginTop="60dp"> TableLayout  android:id="@+id/add_table"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:paddingTop="40dp"> TableRow> TextView  android:text="Student ID:"  android:padding="3dip" /> EditText  android:id="@+id/sid"  android:layout_width="match_parent"  android:layout_height="wrap_content" /> TableRow> TableRow> TextView  android:text="First Name:"  android:padding="3dip" /> EditText  android:id="@+id/fn"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:minWidth="150dip" /> TableRow> TableRow> TextView  android:text="Last Name:"  android:padding="10dip" /> EditText  android:id="@+id/ln"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:minWidth="50dip" /> TableRow> TableRow> TextView  android:text="Course:"  android:padding="10dip" /> EditText  android:id="@+id/cour"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:minWidth="50dip" /> TableRow> TableRow> TextView  android:text="Age:"  android:padding="10dip" /> EditText  android:id="@+id/age"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:minWidth="50dip" /> TableRow> TableRow> TextView  android:text="Address:"  android:padding="10dip" /> EditText  android:id="@+id/addr"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:minWidth="50dip" /> TableRow> TableRow> Button  android:id="@+id/add_button"  android:text="Add Student"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:padding="3dip" /> Button  android:id="@+id/cancel_button"  android:text="Cancel"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:padding="3dip" /> TableRow> TableLayout> Button  android:id="@+id/button2"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="Back"  android:onClick="movetoscreen1"/> LinearLayout> 

//MainActivity.java

 package com.user.myproject; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle(null); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } public void addRecord(View view) { Intent intent = new Intent(MainActivity.this, Screen2.class); startActivity(intent); } @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_main, 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(); /oinspection SimplifiableIfStatement  if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 

//Screen2.java

 package com.user.myproject; import android.content.Context; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; 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.ArrayAdapter; import android.widget.ListView; 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; import android.content.Intent; import android.widget.Toast; public class Screen2 extends AppCompatActivity { private DatabaseManager mydManager; private ListView studentRec; private EditText sid, fn, ln, cour,age, addr; private Button addButton; private TableLayout addLayout; private boolean recInserted; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle(null); mydManager = new DatabaseManager(Screen2.this); // studentRec = (ListView)findViewById(R.id.studentrec);  addLayout = (TableLayout)findViewById(R.id.add_table); addLayout.setVisibility(View.GONE); addButton = (Button) findViewById(R.id.add_button); addButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { sid = (EditText)findViewById(R.id.sid); fn= (EditText)findViewById(R.id.fn); ln = (EditText)findViewById(R.id.ln); cour = (EditText)findViewById(R.id.cour); age= (EditText)findViewById(R.id.age); addr = (EditText)findViewById(R.id.addr); recInserted = mydManager.addRow(Integer.parseInt(sid.getText().toString()), fn.getText().toString(), ln.getText().toString(), cour.getText().toString(),Integer.parseInt(age.getText().toString()), addr.getText().toString() ); addLayout.setVisibility(View.GONE); if (recInserted) { Toast.makeText(Screen2.this, "The row in the students table is inserted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Screen2.this, "Sorry, errors when inserting to DB", Toast.LENGTH_SHORT).show(); } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //imm.hideSoftInputFromWindow(price.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  mydManager.close(); sid.setText(""); fn.setText(""); ln.setText(""); cour.setText(""); age.setText(""); addr.setText(""); studentRec.setAdapter(null); } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } public boolean insertRec() { addLayout.setVisibility(View.VISIBLE); studentRec.setVisibility(View.GONE); return true; } public void movetoscreen1(View view) { Intent intent = new Intent(Screen2.this, MainActivity.class); startActivity(intent); } } 

//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 android.widget.RadioButton; import java.util.ArrayList; public class DatabaseManager { public static final String DB_NAME = "Student"; public static final String DB_TABLE = "studentInfo"; public static final int DB_VERSION = 1; private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (studentid INTEGER PRIMARY KEY, firstname TEXT,lastname TEXT, course TEXT, age INTEGER, address TEXT);"; 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 sid, String fn, String ln, String cour, Integer age, String addr) { synchronized(this.db) { ContentValues newStudent = new ContentValues(); newStudent.put("studentid", sid); newStudent.put("firstname", fn); newStudent.put("lastname", ln); newStudent.put("course", cour); newStudent.put("age", age); newStudent.put("address", addr); 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 ArrayList retrieveRows() { ArrayList studentRows = new ArrayList(); String[] columns = new String[] {"studentid", "firstname", "lastname","course", "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.getInt(4) + "," + cursor.getString(5)); cursor.moveToNext(); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return studentRows; } public void clearRecords() { db = helper.getWritableDatabase(); db.delete(DB_TABLE, 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); } @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); onCreate(db); } } } 
8:00 myproject Student ID First Name: Last Name: Course: Age Address ADD STUDENT CANCEL BACK

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Statistical And Scientific Database Management International Working Conference Ssdbm Rome Italy June 21 23 1988 Proceedings Lncs 339

Authors: Maurizio Rafanelli ,John C. Klensin ,Per Svensson

1st Edition

354050575X, 978-3540505754

More Books

Students also viewed these Databases questions