I am trying to make a form that includes students details and again show that detail in text views. When the user types the details in edit texts save them into the model class then show them again in text views. first, I have created a model class to save variables of students.
Logcat:
2021-11-09 13:12:47.784 30040-30040/com.example.studentapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.studentapp, PID: 30040
java.lang.NumberFormatException: For input string: "stephen"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.studentapp.MainActivity$1.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:6291)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
XML file:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:autofillHints="" />
<EditText
android:id="#+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:autofillHints="" />
<EditText
android:id="#+id/edit_grade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Grade"
android:autofillHints="" />
<EditText
android:id="#+id/edit_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
/>
<EditText
android:id="#+id/edit_distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Distance"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtGrade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<Button
android:text="View"
android:id="#+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Model class:
package com.example.studentapp;
public class Student {
public String name = "";
public int age = 0;
public int grade = 0;
public String address = "";
public double distance = 0;
}
This is .java file:
package com.example.studentapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button buttonView;
EditText editName;
EditText editAge;
EditText editGrade;
EditText editAddress;
EditText editDistance;
TextView textName, textAge, textGrade, textAddress, textDistance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.edit_name);
editAge = findViewById(R.id.edit_age);
editGrade = findViewById(R.id.edit_grade);
editAddress = findViewById(R.id.edit_address);
editDistance = findViewById(R.id.edit_distance);
textName = findViewById(R.id.txtName);
textAge = findViewById(R.id.txtAge);
textGrade = findViewById(R.id.txtGrade);
textAddress = findViewById(R.id.txtAddress);
textDistance = findViewById(R.id.txtDistance);
buttonView = findViewById(R.id.btnView);
buttonView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Student student1 = new Student();
student1.name = editName.getText().toString();
student1.age = Integer.parseInt(editAge.getText().toString());
student1.grade = Integer.parseInt( editName.getText().toString());
student1.address = editName.getText().toString();
student1.distance = Double.parseDouble( editName.getText().toString());
textName.setText(student1.name);
textAge.setText(""+student1.age);
textGrade.setText(student1.grade+" Years");
textAddress.setText(student1.address);
textDistance.setText(student1.distance+"km");
}
});
}
}
Please, bear in mind that I am a very beginner at android development.
student1.distance = Double.parseDouble(editName.getText().toString());
You are using editName for getting distance grade and address. Please, change them according to your needs.
Your code
student1.grade = Integer.parseInt( editName.getText().toString());
student1.distance = Double.parseDouble( editName.getText().toString());
Expected code
student1.grade = Integer.parseInt( editGrade.getText().toString());
student1.distance = Double.parseDouble( editDistance.getText().toString());
Your are trying to parse string to integer Integer.parseInt( editName.getText().toString()). It won't work for non numbers input.
Looking at your Locat message, it seems you're saving the String name in an Integer variable, kindly look at your codebase to see if you're making that mistake, if so reassign the name variable to a string variable.
You are using wrong editText for grade and distance.
your code:
student1.grade = Integer.parseInt(editName.getText().toString());
change editName to editGrade :
student1.grade = Integer.parseInt(editGrade.getText().toString());
aswell for:
student1.distance = Double.parseDouble(editName.getText().toString());
change editName to editDistance:
student1.distance = Double.parseDouble(editDistance.getText().toString());
Related
This Java takes the inputs of Edit Text fields and outputs them into Toast.
I have no idea where or what the error is, any help would be greatly appreciated:
Java
package colonyapplication.colony;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class tenantReg extends Activity {
EditText fullName = (EditText) findViewById(R.id.etFname);
EditText userEmail = (EditText) findViewById(R.id.etEmail);
EditText userPass = (EditText) findViewById(R.id.etPass);
String fName = fullName.getText().toString();
String uEmail = userEmail.getText().toString();
String uPass = userPass.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tenant_reg);
}
public void btnSubmit() {
String tenant = "Tenant";
Toast.makeText(tenantReg.this, "UserName is: " + fName + ". Password is: " + uPass + "email is: " + uEmail + tenant, Toast.LENGTH_LONG).show();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#fcdfaa"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="colonyapplication.colony.tenantReg">
<ImageView
android:id="#+id/imageView2"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/colonylogo"
android:layout_above="#+id/passTextBox"
android:layout_centerHorizontal="true"
android:layout_marginBottom="61dp" />
<EditText
android:id="#+id/etFname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_below="#+id/imageView2"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etFname"
android:layout_alignStart="#+id/etFname"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/etPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="#+id/etEmail"
android:layout_alignStart="#+id/etEmail"
android:layout_alignEnd="#+id/etEmail" />
<EditText
android:id="#+id/etPassconfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword"
android:layout_below="#+id/etPass"
android:layout_alignStart="#+id/etPass"
android:layout_alignEnd="#+id/etPass" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="47dp"
android:background="#EE7600"
android:onClick="btnSubmit"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="17sp"
android:layout_below="#+id/etPassconfirm"
android:layout_alignStart="#+id/etPassconfirm" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:background="#EE7600"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="17sp"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignEnd="#+id/etPassconfirm"
android:layout_marginEnd="33dp" />
</RelativeLayout>
For whatever reason, when the submit button is clicked, the program stops in the emulator. The error in the Android Monitor states: "java.lang.IllegalStateException: Could not find method btnSubmit(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'"
As I see you have 2 Button but I dont know which one is submit button.
well also I can not find the meaning of public void btnSubmit(). As I know you should define tn submit:
Button btnSubmit = (Button) findViewById(R.id.your_btnSubmit_id);
and then use onClickListener method to do somthing when btn clicked
btnsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do somthing
}
});
Edit:
You can use your code just replace
public void btnSubmit()
with this:
public void btnSubmit(View v)
I am realitavely new to Java coding and am trying to use a button press (in an Android App) to update a text view. The code I am currently using is causing my app to crash whenever I try to press the button.
Here is my XML
<LinearLayout>
<TextView
android:id="#+id/myTextView_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculateNewNumber"
android:text="Calculate New Number" />
</LinearLayout>
And Here is my Java
package com.example.android.myapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int startNumber = 0;
public void calculateNewNumber(View view) {
startNumber = startNumber + 1;
display(startNumber);
}
private void display(int number) {
TextView myTextView = (TextView) findViewById(
R.id.myTextView_text_view);
myTextView.setText("" + number);
}
}
Thanks for any help you can give.
The code itself works fine so the problem must be with your XML, assuming you have provided it in full here.
Using this XML, your MainActivity code works fine for me.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="#+id/myTextView_text_view"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculateNewNumber"
android:text="Calculate New Number" />
</LinearLayout>
</RelativeLayout>
I am new to Android development and I have an issue with making Android EditText vertically scroll able. The vertical scrolling of the EditText does not work for me. I followed several posts and resources but none of them worked for me.
I am using the following code sample to enable the vertical scroll ability.
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
The following are the activity class and layout resource file, respectively I am using in my app.
CreateAppointmentActivity.java
package lk.iit.appointmentmanagerapp.activities;
import java.sql.Date;
import java.sql.Time;
import lk.iit.appointmentmanagerapp.data.Appointment;
import lk.iit.appointmentmanagerapp.data.DatabaseHandler;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Scroller;
public class CreateAppointmentActivity extends Activity {
private EditText titleEditText;
private EditText timeEditText;
private EditText detailsEditText;
private EditText dateEditText;
private Button saveButton;
private Button resetButton;
private final DatabaseHandler handler = new DatabaseHandler(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_appointment);
this.titleEditText = (EditText)(this.findViewById(R.id.edit_title));
this.timeEditText = (EditText)(this.findViewById(R.id.edit_time));
this.detailsEditText = (EditText)(this.findViewById(R.id.edit_details));
this.dateEditText = (EditText)(this.findViewById(R.id.edit_date));
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
SharedPreferences preferences = getSharedPreferences("date_variables", MODE_PRIVATE);
this.dateEditText.setText(preferences.getInt("Year", 0) + "-" + preferences.getInt("Month", 0) + "-" + preferences.getInt("Day", 0));
this.dateEditText.setEnabled(false);
this.saveButton = (Button)(this.findViewById(R.id.save_new_appointment_button));
this.saveButton.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
String[] dateComponents = dateEditText.getText().toString().split("-");
String[] timeComponents = timeEditText.getText().toString().split(":");
Appointment appointment = new Appointment();
appointment.setAppointment_title(titleEditText.getText().toString());
appointment.setAppointment_date(new Date(Integer.parseInt(dateComponents[0]), Integer.parseInt(dateComponents[1]), Integer.parseInt(dateComponents[2])));
appointment.setAppointment_time(new Time(Integer.parseInt(timeComponents[0]), Integer.parseInt(timeComponents[1]), Integer.parseInt(timeComponents[2])));
appointment.setAppointment_details(detailsEditText.getText().toString());
boolean inserted = handler.addAppointment(appointment);
//
//
}
});
this.resetButton = (Button)(this.findViewById(R.id.reset_button));
this.resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
titleEditText.setText("");
timeEditText.setText("");
detailsEditText.setText("");
dateEditText.setText("");
}
});
}
}
activity_create_appointment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/background"
tools:context="lk.iit.appointmentmanagerapp.activities.CreateAppointmentActivity" >
<TextView
android:id="#+id/create_activity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_create"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:textSize="18.5sp"
android:textColor="#ffffff" />
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:text="#string/title_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:layout_toRightOf="#+id/title_textview"
android:layout_marginLeft="30dip"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_textview"
android:layout_marginTop="25dip"
android:text="#string/time_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="#+id/edit_time"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/time_textview"
android:layout_below="#+id/edit_title"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="time" />
<TextView
android:id="#+id/details_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_textview"
android:text="#string/details_textview_text"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/details_textview"
android:layout_alignBottom="#+id/details_textview"
android:layout_alignLeft="#+id/edit_title"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
</EditText>
<TextView
android:id="#+id/date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/details_textview"
android:text="Date"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/date_textview"
android:layout_below="#+id/edit_details"
android:background="#ffffff"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="date" >
</EditText>
<Button
android:id="#+id/save_new_appointment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date_textview"
android:layout_marginTop="15dip"
android:layout_marginBottom="10dip"
android:layout_centerHorizontal="true"
android:text="#string/save_button_text"
android:textAllCaps="false" />
<Button
android:id="#+id/reset_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/save_new_appointment_button"
android:layout_marginTop="2dip"
android:layout_centerHorizontal="true"
android:text="#string/reset_button_text"
android:textAllCaps="false" />
</RelativeLayout>
Please bear with me if I have done any mistakes as I am relatively new to this area of development.
I would be grateful if someone help me out with this issue.
Try this in java code:
EditText dwEdit = (EditText) findViewById(R.id.DwEdit);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
And in your xml:
<EditText
android:id="#+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText>
If you wanna scroll editText only in detailEditText then you specify height of editText 50dp or your need and remove inputType="text" from your editText ....it fulfill your need..
and you don't need any code in java relative to that editText..
I am a new Android programmer. I am entering data into an SQLite database, and I want my ListView to then refresh and show the information that I have just added to the database. The ListView is below the add button. The logcat error shows a NullPointerException.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.34"
android:text="Registration No:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_reg"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:ems="10"
/>
<requestFocus />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.43"
android:text="Mileage:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_mileage"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_weight="0.29"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout8"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_weight="0.51"
android:text="Budget Amount:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_budget"
android:layout_width="167dp"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout9"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.37"
android:text="Tank Capacity: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_capacity"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/add_button_vehicle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Vehicle" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.43"
android:orientation="vertical" >
<ListView
android:id="#+id/vehicleList_reg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</LinearLayout>
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.example.budgetfuel.databasemodel.Car;
import com.example.budgetfuel.databasemodel.DBhelper;
import com.example.budgetfuel.databasemodel.DatabaseHelper;
import com.example.budgetfuel.databasemodel.SQLController;
public class VehicleSetup extends FragmentActivity {
Button addvehicle;
ListView lv;
SQLController dbcon;
Button btnAdd;
EditText reg, milge, cap, budgt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_settings);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
dbcon = new SQLController(this);
dbcon.open();
addvehicle = (Button) findViewById(R.id.add_button_vehicle);
lv = (ListView) findViewById(R.id.listViewVehicles);
reg = (EditText) findViewById(R.id.vehicle_reg);
milge = (EditText) findViewById(R.id.vehicle_mileage);
cap = (EditText) findViewById(R.id.vehicle_budget);
budgt = (EditText) findViewById(R.id.vehicle_capacity);
// Attach The Data From DataBase Into ListView Using Crusor Adapter
Cursor cursor = dbcon.readData();
String[] from = new String[] {DBhelper.VEHICLE_ID,DBhelper.VEHICLE_REG, DBhelper.VEHICLE_BUDGET, DBhelper.VEHICLE_CAPACITY };
int[] to = new int[] { R.id.vehicle_id, R.id.vehicle_reg, R.id.vehicle_mileage, R.id.vehicle_budget,
R.id.vehicle_capacity };
#SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
VehicleSetup.this, R.layout.view_car_entry, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
//
addvehicle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String registration = reg.getText().toString();
int mileage = Integer.parseInt(milge.getText().toString());
int capacity = Integer.parseInt(cap.getText().toString());
double budget = Double.parseDouble(budgt.getText().toString());
dbcon.insertData(registration, mileage, budget, capacity);
reg.setText("");
milge.setText("");
cap.setText("");
budgt.setText("");
}
});
}
try to put adapter.notifyDataSetChanged(); after lv.setAdapter(adapter);
EDIT: Thank you all for your help. I edited my Database class to contain the following
final EditText firstName = (EditText) findViewById(R.id.editText1); // First Name
final EditText middleName = (EditText) findViewById(R.id.editText2); // Middle Name
final EditText birthDate = (EditText) findViewById(R.id.editText4); // Birth Date
final String firstname = firstName.getText().toString(); // First Name
final String middlename = middleName.getText().toString(); // Middle Name
final String birthdate = birthDate.getText().toString(); // Birth Date
TextView firstNameText = (TextView)findViewById(R.id.firstname);
TextView middleNameText = (TextView)findViewById(R.id.middlename);
TextView birthDateText = (TextView)findViewById(R.id.birthdate);
firstNameText.setText(firstname);
middleNameText.setText(middlename);
birthDateText.setText(birthdate);
and my database.xml now shows
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:background="#aa0000"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/firstname"/>
<TextView
android:background="#00aa00"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/middlename"/>
<TextView
android:background="#0000aa"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/birthdate"/>
</TableRow>
but when I run the emulator and I try to access that screen (by clicking a button from the previous screen) the application crashes? I set up the button correctly using the OnClickListener so I'm fairly sure the button is not the problem
If you're having trouble understanding the interface here's an example you can cut and paste.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name: "
android:id="#+id/firstLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/firstEdit"
android:text="John"
android:layout_toRightOf="#id/firstLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Middle Name: "
android:id="#+id/middleLabel"
android:layout_below="#id/firstLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/middleEdit"
android:text="Phillip"
android:layout_below="#id/firstEdit"
android:layout_toRightOf="#id/middleLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name: "
android:id="#+id/lastLabel"
android:layout_below="#id/middleLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/lastEdit"
android:text="Doe"
android:layout_below="#id/middleEdit"
android:layout_toRightOf="#id/lastLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthdate: "
android:id="#+id/birthLabel"
android:layout_below="#id/lastLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/birthEdit"
android:text="08/09/1977"
android:layout_toRightOf="#id/lastLabel"
android:layout_below="#id/lastEdit"
android:layout_alignParentRight="true"></EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#id/birthEdit">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:background="#aa0000"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something"
android:id="#+id/firstTable"/>
<TextView
android:background="#00aa00"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something1"
android:id="#+id/middleTable"/>
<TextView
android:background="#0000aa"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something2"
android:id="#+id/birthTable"/>
</TableRow>
</LinearLayout>
</RelativeLayout>
DatabaseExample.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class DatabaseExample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Setup edit fields
EditText firstEdit = (EditText)findViewById(R.id.firstEdit);
EditText middleEdit = (EditText)findViewById(R.id.middleEdit);
EditText lastEdit = (EditText)findViewById(R.id.lastEdit);
EditText birthEdit = (EditText)findViewById(R.id.birthEdit);
//Get the text and store in variables
String firstName = firstEdit.getText().toString();
String middleName = middleEdit.getText().toString();
String lastName = lastEdit.getText().toString();
String birthDate = birthEdit.getText().toString();
//setup the text fields
TextView firstTable = (TextView)findViewById(R.id.firstTable);
TextView middleTable = (TextView)findViewById(R.id.middleTable);
TextView birthTable = (TextView)findViewById(R.id.birthTable);
//change the text fields
firstTable.setText(firstName);
middleTable.setText(middleName);
birthTable.setText(birthDate);
}
}
Once again this would be just the interface for inputting and displaying data. Instead of storing the data in String variables you would use SQLite, SharedPreferences, or write to a file on the SDCard.
Try as follows
final EditText firstName = (EditText) findViewById(R.id.editText1);
TextView firstNameTxt = (TextView)findViewById(R.id.firstname);
firstNameTxt.setText(firstName);
You cannot store values from the user in the strings.xml file. You must have a data store such as SharedPreferences or SQLite. Here is another link for help on data storage with Android.