I have this 3 types of edit text input.
I want to compare the 3 edit text to get the FreeTime.
xml codes
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/morningLabel"
android:id="#+id/MorningLabel"
android:textStyle="bold"
android:layout_alignTop="#+id/textView2"
android:layout_alignRight="#+id/MorningText"
android:layout_alignEnd="#+id/MorningText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Working Hour(s)"
android:id="#+id/textView3"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Afternoon"
android:id="#+id/textView2"
android:textStyle="bold"
android:layout_alignTop="#+id/textView5"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Evening"
android:id="#+id/textView5"
android:textStyle="bold"
android:layout_marginBottom="41dp"
android:layout_above="#+id/AfternoonText"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 6.01 a.m. \n to \n12.00 p.m."
android:id="#+id/morningTime"
android:textStyle="italic"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/textView3"
android:layout_toStartOf="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 12.01 p.m. \n to \n 6.00 p.m."
android:id="#+id/afternoonTime"
android:textStyle="italic"
android:layout_alignTop="#+id/morningTime"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" 6.01 p.m. \n to \n12.00 a.m."
android:id="#+id/eveningTime"
android:textStyle="italic"
android:layout_alignTop="#+id/afternoonTime"
android:layout_alignLeft="#+id/EveningText"
android:layout_alignStart="#+id/EveningText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:id="#+id/btnNext"
android:layout_alignParentBottom="true"
android:layout_marginBottom="38dp"
android:layout_alignRight="#+id/eveningTime"
android:layout_alignEnd="#+id/eveningTime"
android:layout_alignLeft="#+id/morningTime"
android:layout_alignStart="#+id/morningTime"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/MorningText"
android:hint="0-6"
android:layout_marginBottom="22dp"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_above="#+id/morningTime"
android:layout_toLeftOf="#+id/textView3"
android:layout_alignLeft="#+id/morningTime"
android:layout_alignStart="#+id/morningTime"
android:text="#string/MorningInput"
android:singleLine="false"
android:textAlignment="center"
android:numeric="integer" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/AfternoonText"
android:hint="0-6"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_alignTop="#+id/MorningText"
android:layout_alignLeft="#+id/afternoonTime"
android:layout_alignStart="#+id/afternoonTime"
android:layout_alignRight="#+id/afternoonTime"
android:layout_alignEnd="#+id/afternoonTime"
android:text="#string/AfternoonInput"
android:textAlignment="center"
android:numeric="integer" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/EveningText"
android:hint="0-6"
android:autoText="true"
android:background="#ffd1d1d1"
android:editable="false"
android:textSize="#dimen/abc_text_size_display_1_material"
android:layout_alignBaseline="#+id/AfternoonText"
android:layout_alignBottom="#+id/AfternoonText"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:text="#string/EveningInput"
android:textAlignment="center"
android:numeric="integer"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/timeLabel"
android:id="#+id/TimeText"
android:layout_above="#+id/btnNext"
android:layout_alignLeft="#+id/btnNext"
android:layout_alignStart="#+id/btnNext" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c"
android:onClick="calculateClickHandler"
android:id="#+id/btnc"
android:layout_above="#+id/TimeText"
android:layout_toRightOf="#+id/afternoonTime"
android:layout_toEndOf="#+id/afternoonTime"
android:layout_marginBottom="42dp" />
This is the java code. I think I put the wrong if else. How can I solve this? Anyone help:
public class WorkingHour extends Activity
{
public void calculateClickHandler(View view)
{
if (view.getId() == R.id.btnc)
{
Intent i=new Intent(this, Result.class);
// get the references to the widgets
EditText MorningInput = (EditText)findViewById(R.id.MorningText);
EditText AfternoonInput = (EditText)findViewById(R.id.AfternoonText);
EditText EveningInput = (EditText)findViewById(R.id.EveningText);
TextView TimeText = (TextView)findViewById(R.id.TimeText);
// get the users values from the widget references
int morninghour = Integer.parseInt(MorningInput.getText().toString());
int afternoonhour = Integer.parseInt(AfternoonInput.getText().toString());
int eveninghour = Integer.parseInt(EveningInput.getText().toString());
// calculate the free time
int freeTimeValue = calculateFreeTime(morninghour,afternoonhour,eveninghour);
// interpret the meaning of the bmi value
String freetimeInterpretation = interpretFT(freeTimeValue);
// now set the value in the result text
TimeText.setText(freeTimeValue + "-" + freetimeInterpretation);
SharedPreferences sp2 = getSharedPreferences("name2",MODE_PRIVATE);
SharedPreferences.Editor editor=sp2.edit();
editor.putString("rslt 3",TimeText.getText().toString());
editor.commit();
}
}
// the formula to calculate the free time
private int calculateFreeTime (int morninghour, int afternoonhour, int eveninghour)
{
if (((6- morninghour)> (6-afternoonhour)) && ((6- morninghour)> (6-eveninghour)))
{
return (int) (6 - morninghour);
}
else if (((6- afternoonhour)> (6-morninghour)) && ((6- afternoonhour)> (6-eveninghour)))
{
return (int) (6 - afternoonhour);
}
else if (((6- eveninghour)> (6-morninghour)) && ((6-eveninghour)> (6-afternoonhour)))
{
return (int) (6 - eveninghour);
}
return 0;
}
This code:
if (freetime < )
{
return "morning";
}
Is definitely wrong and should not even compile. You are saying here: if freetime is smaller than nothing... do something. You have to put a value in it (these are for example):
if (freetime < 6)
{
return "morning";
}
else if (freetime < 12)
{
return "Afternoon";
}
Also, there is no need to cast this return value because it are both integers:
return (int) (6 - morninghour);
^^
If you want to make your code a little better (mainly for readability), you can optimize this:
morninghour = 6 - morninghour;
afternoonhour = 6 - afternoonhour;
eveninghour = 6 - eveninghour;
if ((morninghour > afternoonhour) && (morninghour > eveninghour))
{
return morninghour;
}
else if ((afternoonhour > morninghour) && (afternoonhour > eveninghour))
{
return afternoonhour;
}
else if ((eveninghour > morninghour) && (eveninghour > afternoonhour))
{
return eveninghour;
}
return 0;
Related
I am developing an app to help people know when it is time for a phone upgrade. I ran into a little problem along the way, mainly when trying to test the functionality of the code. I rated all 6 categories and pressed the continue button. Only the rating for the "Game" shows up. I don't know what is going on since I have all six textviews included.
Shows only one rating
Java:
package com.inducesmile.phoneupgrade;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class Page1 extends Activity {
TextView tvBatt, tvPerf, tvAttr, tvCam, tvVal, tvGame;
RatingBar rateBattery, ratePerformance, rateCamera, rateValue, rateGaming, rateAttractiveness;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Init();
}
public void Init() {
tvBatt = (TextView) findViewById(R.id.tvBatt);
tvPerf = (TextView) findViewById(R.id.tvPerf);
tvAttr = (TextView) findViewById(R.id.tvAttr);
tvCam = (TextView) findViewById(R.id.tvCam);
tvVal = (TextView) findViewById(R.id.tvVal);
tvGame = (TextView) findViewById(R.id.tvGame);
rateBattery = (RatingBar) findViewById(R.id.rateBatt);
rateCamera = (RatingBar) findViewById(R.id.rateCam);
rateGaming = (RatingBar) findViewById(R.id.rateGame);
rateValue = (RatingBar) findViewById(R.id.rateValue);
ratePerformance = (RatingBar) findViewById(R.id.Performance);
rateAttractiveness = (RatingBar) findViewById(R.id.rateAttr);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.btnCont) {
String strBatt = String.valueOf(rateBattery.getRating());
tvBatt.setText(strBatt);
String strAttr = String.valueOf(rateAttractiveness.getRating());
tvAttr.setText(strAttr);
String strCam = String.valueOf(rateCamera.getRating());
tvCam.setText(strCam);
String strPerf = String.valueOf(ratePerformance.getRating());
tvPerf.setText(strPerf);
String strGame = String.valueOf(rateGaming.getRating());
tvGame.setText(strGame);
String strVal = String.valueOf(rateValue.getRating());
tvVal.setText(strVal);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:background="#FFF000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="How Important Are These Features to You?"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="#000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue"
android:textColor="#FFFFFF"
android:id="#+id/btnCont"
android:background="#drawable/buttonshape"
android:onClick="onButtonClick"
android:layout_below="#+id/Performance"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/textView2" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateCam"
android:layout_alignLeft="#+id/rateGame"
android:layout_alignStart="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateGame" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/rateCam" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateValue"
android:layout_below="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Performance"
android:layout_below="#+id/rateValue"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Battery Life:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gaming:"
android:id="#+id/textView4"
android:layout_alignTop="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Camera:"
android:id="#+id/textView5"
android:layout_below="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Attractiveness:"
android:id="#+id/textView6"
android:layout_marginTop="14dp"
android:layout_alignTop="#+id/rateAttr"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Value:"
android:id="#+id/textView7"
android:layout_alignTop="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Performance:"
android:id="#+id/textView8"
android:layout_below="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBatt"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvGame"
android:layout_alignBottom="#+id/textView4"
android:layout_toRightOf="#+id/textView4"
android:layout_toEndOf="#+id/textView4"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvCam"
android:layout_alignBottom="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:layout_toEndOf="#+id/textView5"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvAttr"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvVal"
android:layout_alignBottom="#+id/textView7"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPerf"
android:layout_alignBottom="#+id/textView8"
android:layout_toRightOf="#+id/textView8"
android:layout_toEndOf="#+id/textView8"
android:text="____" />
Your code is fine, try rebuild/clean the project.
If you're using Android Studio, go to Build -> Clean Project and then Build -> Rebuild Project.
I am adding TextViews to a simple application, running some code to determine what text should show in each one, then displaying it based on what a user previously entered in a bundle.
The problem is no text is showing up at all!
I am running code in the onCreate method that determines what text should show based on passed in values from a previous activity in a bundle. The values seem to pass in without an error and the other Textviews in the activity reflect the changes, but not the income, expenses or incomeExpenses TextView.
Here is my code:
package androidbro.costoflivingcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.*;
import java.io.Console;
public class FinalGrade extends ActionBarActivity {
//Grade and message strings
public String myGrade;
public String finalMessage;
//Doubles for calculated values
public double rentMortgage;
public double utilities;
public double insurance;
public double phoneInternet;
public double food;
public double carPayment;
public double misc;
public double myIncome;
public double totalExpenses;
public double expensesToIncome;
//Calculate grade based on expenses to income ratio
public String getGrade(double expensesToIncome) {
String grade = "A";
if (expensesToIncome<=0.3) {
grade = "A";
} else if (expensesToIncome<=0.4){
grade = "B";
} else if (expensesToIncome<=0.6) {
grade = "C";
} else if (expensesToIncome<=0.8) {
grade = "D";
} else {
grade = "F";
}
return grade;
}
//Determine final message
public String getFinalMessage(String grade){
String finalMessage;
if (grade == "A"){
finalMessage = "You are living well within your means! You could probably afford to spend more. Either way, great work!";
} else if(grade == "B"){
finalMessage = "You are doing great! Your expenses are well below your income. There are a few areas to improve, but good work!";
} else if(grade == "C") {
finalMessage = "Not too bad, but not great either. You might be in trouble if you lost your income. Try to improve a little!";
} else if(grade == "D") {
finalMessage = "Uh oh! You are spending well over half the money you earn! Consider saving and investing more or it could be trouble.";
} else {
finalMessage = "Oh no! You are spending almost all your income! Save a little and live within your means or else you'll be broke!";
}
return finalMessage;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_grade);
//Income, expenses and bundles
Bundle b = getIntent().getExtras();
rentMortgage = b.getDouble("rentmortgage");
utilities = b.getDouble("utilities");
insurance = b.getDouble("insurance");
phoneInternet = b.getDouble("car");
food = b.getDouble("phone");
carPayment = b.getDouble("food");
misc = b.getDouble("misc");
myIncome = b.getDouble("myincome");
totalExpenses = rentMortgage+utilities+insurance+phoneInternet+food+carPayment+misc;
expensesToIncome = totalExpenses/myIncome;
//Set final message and grade values
myGrade = getGrade(expensesToIncome);
finalMessage = getFinalMessage(myGrade);
//TextViews for setting values
TextView gradeView = (TextView)findViewById(R.id.gradeView);
TextView messageView = (TextView)findViewById(R.id.messageView);
TextView income = (TextView)findViewById(R.id.income);
TextView expenses = (TextView)findViewById(R.id.expenses);
TextView incomeExpenses = (TextView)findViewById(R.id.incomeExpenses);
//Display final grade and message
gradeView.setText(myGrade);
messageView.setText(finalMessage);
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
}
#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_final_grade, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Display expenses, income and expenses to income percentage on final grade
}
And here is my XML:
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<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="ericleeconklin.costoflivingcalculator.FinalGrade">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Grade:"
android:id="#+id/textView2"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="A"
android:id="#+id/gradeView"
android:textSize="85dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:id="#+id/messageView"
android:layout_below="#+id/gradeView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Monthly Income:"
android:id="#+id/yourIncome"
android:layout_below="#+id/messageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/income"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourIncome"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="sdsdfsd"
android:textColor="#color/abc_background_cache_hint_selector_material_dark"
android:textIsSelectable="true"
android:textSize="23dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses:"
android:id="#+id/yourExpenses"
android:paddingTop="20dp"
android:layout_below="#+id/income"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/yourIncome"
android:layout_alignEnd="#+id/yourIncome"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/incomeExpenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/textView4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/expenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourExpenses"
android:layout_alignLeft="#+id/incomeExpenses"
android:layout_alignStart="#+id/incomeExpenses"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses/Income"
android:id="#+id/textView4"
android:paddingTop="20dp"
android:layout_below="#+id/yourExpenses"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/incomeExpenses"
android:layout_toStartOf="#+id/incomeExpenses"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Here are some tips:"
android:id="#+id/textView5"
android:paddingTop="350dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</ScrollView>
Instead of this:
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
Do this:
income.setText(String.valueOf(myIncome);
expenses.setText(String.valueOf(totalExpenses));
incomeExpenses.setText(String.valueOf(expensesToIncome));
I have updated my answer
For passing double value between Activity:
Intent inent = new Intent(thisActivity.this, nextActivity.class);
Bundle b = new Bundle();
b.putDouble("myincome", myIncome); // myIncome is your double value here
intent.putExtras(b);
startActivity(intent);
To get value in next Activity:
Bundle b = getIntent().getExtras();
double myIncome = b.getDouble("myincome");
Check your procedure of passing double value, I have mentioned it above
Edited XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_BigHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Your Grade:"
android:textSize="40sp" />
<TextView
android:id="#+id/tv_Grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_BigHeading"
android:layout_centerHorizontal="true"
android:text="A"
android:textSize="80sp"
android:textStyle="bold" />
<TextView
android:id="#+id/messageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_Grade"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:textSize="14sp" />
<TextView
android:id="#+id/yourIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Monthly Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/incomeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourIncome"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/yourExpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourIncome"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/expenseValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/incomeValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourExpenses"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourExpenses"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses/Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income_Value"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_below="#+id/expenseValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/Expenses_Income"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Expenses_Income"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Here are some tips:"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
This will solve your issue now, and use all my recommendations e.g: income.setText(String.valueOf(myIncome);
for setting the textview values
So I have been trying to develop an app that adds a select quantity together in a menu, all while in Android studio/eclipse.
I am listing off drinks and to the right of each drink is a "-" (the minus) and "+" (the plus) signs, which are buttons respectively, with a "0" in the middle.
At the bottom of the screen, I have a "Total" section.
I have a screen shot of what the menu appears as thus far:
I am looking to have the + and - buttons affect the number that lies in the middle of them. So when the "+" is pressed it adds 1 and if pressed again, it adds 1 and so on, the "-" button will take 1 away from the number back to zero if need be.
It should be able to tally the total price of each drink all together and output the sum to the
"$ 0.00" near the total. The "Order" button just takes me to another screen, which will be tackled at a later date.
I have tried searching for a good many hours and there are so many links I am drowning that I can not find any help that sticks.
I know that the Java code should use the ID of the EditText's but I am confused as to how.
I will post what code I have for the xml and java code. If there is also a manifest I have to add, I can not find it.
drinkmenu.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/mainmenulayout"
android:background="#34D6D9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Drinks:"
android:id="#+id/textView"
android:background="#FFFFFF"
android:textStyle="bold"
android:textSize="24dp"
android:layout_toStartOf="#+id/textView2"
android:layout_toLeftOf="#+id/textView2"
android:layout_marginRight="75dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Quantity:"
android:id="#+id/textView2"
android:textSize="24dp"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:layout_alignEnd="#+id/orderbtn" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Espresso"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Macciato"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Con Panna"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Americano"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Latte"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button6"
android:layout_alignTop="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffff45df"
android:textSize="24dp" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_above="#+id/button2" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button7"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button8"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24sp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_alignBottom="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button9"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button10"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/button8"
android:layout_alignEnd="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button11"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button12"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_toStartOf="#+id/button10"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_above="#+id/button4" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText6"
android:layout_toStartOf="#+id/button11"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_alignBottom="#+id/button11" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText7"
android:layout_toStartOf="#+id/button12"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_alignBottom="#+id/button12" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button14"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button15"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button16"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:text="Total:"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textView"
android:layout_toStartOf="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText9"
android:layout_alignBottom="#+id/editText8"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:text=" $ 0.00"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignTop="#+id/editText8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:id="#+id/orderbtn"
android:textSize="24sp"
android:background="#drawable/buttonround"
android:layout_alignBottom="#+id/editText9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
</RelativeLayout>
java code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DrinkMenu extends Activity {
Button button;
Button buttton6; Button buttton7; Button buttton8; Button buttton9;
Button buttton10; Button buttton11; Button buttton16; Button buttton14;
Button buttton12; Button buttton15;
// EditText editText2; EditText editText4; EditText editText5;
// EditText editText6; EditText editText7; EditText editText9;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.orderbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ThirdPartyPaymentMethod.class);
startActivity(intent);
}
});
}
}
I already have my manifest to allow button actions.
I know there are methods out there, but my searches have ended in my frustration. Any help would be appreciated. I am new to this, I enjoy the front end, but the back end is new.
I'll do a part and for remaining you have to do.
Consider,
- is a button with id 'button1' and '+' has button2
Quantity is a EditText which has id qty.
Right, coming to the code :
public class MainActivity extends Activity {
EditText quantity;
Button plus,minus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
plus = (Button) findViewById(R.id.button1);
minus = (Button) findViewById(R.id.button2);
quantity = (EditText) findViewById(R.id.qty);
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1+1;
quantity.setText(Integer.toString(inum));
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1-1;
quantity.setText(Integer.toString(inum));
}
});
}
}
For sub total, you just need to multiply quantity and the item price. For the Total Price, you just need to add all the subtotals. Hope it helps!
UPDATE about Catching Zero:
You are getting the quantity here.
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
So, if the number is 0, perform if else
if (num1==0)
{
//Create a alert showing your quantity is zero.
//here i use toast to display
Toast.makeText(this,"Your quantity is Zero. You can't reduce the quantity", Toast.LENGTH_LONG).show();
}
else
{
// do the stuffs here
}
what you want to do is something like this:
Button button = (Button) findViewById(R.id.my_button);
final TextView textView = (TextView) findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener() {
#Overrride
public void onClick(View aView) {
textView.setText("Hi");
}
});
You can write this in the onCreate() method of the Activity. Not tested yet, but this will change the text of the TextView to the desired value. Let me know if I have understood your problem correctly.
I have some simple code where I input two numbers in EditText fields, add them together, and show them in a TextView.
Here is my code:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:visibility="invisible"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/editText2"
android:text="Clear"
android:onClick="Clicked" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="35dp"
android:text="Total"
android:onClick="Clicked" />
And here is my Clicked method:
public void Clicked(View v) {
int total;
EditText t1= (EditText)findViewById(R.id.editText1);
EditText t2= (EditText)findViewById(R.id.editText2);
TextView tv= (TextView)findViewById(R.id.textView1);
if(v.getId()==R.id.button1)
{
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
tv.setText(total);
tv.setVisibility(1);
}
else if (v.getId()==R.id.button2)
{
t1.setText("");
t2.setText("");
}
}
I input two numbers and when I press clear it works fine but the total button does not add. Instead, this happens:
It might be a problem with my integer casting or a problem with logic.
Change this:
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
To this:
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
Its stopped because you may be setting a Int value to setText of TextView. You need to convert it to String
textView.setText(CharSequence text)
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
tv.setText(total+""); //this converts total to string
//tv.setText(total.toString()); //can use either
I'm new in android and I'm having a difficult time in this. all i want to do is to display data from database to List view. The error is always unhandled exception:illegelargumentexception.
My xml is :
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button1"
android:layout_width="117dp"
android:layout_height="29dp"
android:text="SAVE"
android:textSize="8sp"
android:layout_x="17dp"
android:layout_y="186dp" />
<TextView
android:id="#+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:textSize="10sp"
android:layout_x="134dp"
android:layout_y="24dp" />
<TextView
android:id="#+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:textSize="10sp"
android:layout_x="132dp"
android:layout_y="75dp" />
<EditText
android:id="#+id/t1"
android:layout_width="287dp"
android:layout_height="32dp"
android:text="EditText"
android:textSize="12sp"
android:layout_x="15dp"
android:layout_y="43dp" />
<EditText
android:id="#+id/t2"
android:layout_width="287dp"
android:layout_height="34dp"
android:textSize="12sp"
android:layout_x="14dp"
android:layout_y="96dp" />
<TextView
android:id="#+id/mname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Middle Name"
android:textSize="10sp"
android:layout_x="126dp"
android:layout_y="130dp" />
<EditText
android:id="#+id/t3"
android:layout_width="287dp"
android:layout_height="31dp"
android:textSize="12sp"
android:layout_x="13dp"
android:layout_y="149dp" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="10sp"
android:layout_x="139dp"
android:layout_y="191dp" />
<ListView
android:id="#+id/lst"
android:layout_width="320dp"
android:layout_height="172dp"
android:background="#ffccccff"
android:layout_x="0dp"
android:layout_y="258dp" />
<TextView
android:id="#+id/ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="26dp"
android:layout_y="224dp" />
<TextView
android:id="#+id/fnameshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="94dp"
android:layout_y="225dp" />
<TextView
android:id="#+id/lnameshow"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="TextView"
android:layout_x="164dp"
android:layout_y="225dp" />
<TextView
android:id="#+id/mnameshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="230dp"
android:layout_y="227dp" />
<Button
android:id="#+id/btnFind"
android:layout_width="117dp"
android:layout_height="29dp"
android:text="Find"
android:textSize="10sp"
android:layout_x="193dp"
android:layout_y="186dp" />
</AbsoluteLayout>
Code:
void find_Click(object sender, EventArgs e)
{
string sColumnName = "";
if (text1.Text.Trim () != "") {
sColumnName = "fname";
GetCursorView (sColumnName, text1.Text.Trim ());
} else
if (text2.Text.Trim () != "") {
sColumnName = "lname";
GetCursorView (sColumnName, text2.Text.Trim ());
} else
if (text3.Text.Trim () != "") {
sColumnName = "mname";
GetCursorView (sColumnName, text3.Text.Trim ());
}
text.Text = "Search " + sColumnName + ".";
ListView lvTemp = FindViewById<ListView>(Resource.Id.lst);
lvTemp.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(ListView_ItemClick);
}
protected void GetCursorView(string sColumn, string sValue)
{
Android.Database.ICursor icTemp = sql.GetRecordCursor(sColumn, sValue);
if (icTemp != null)
{
icTemp.MoveToFirst();
ListView lvTemp = FindViewById<ListView>(Resource.Id.lst);
string[] from = new string[] { "id", "fname", "lname", "mname" };
int[] to = new int[] {
Resource.Id.ID,
Resource.Id.fnameshow,
Resource.Id.lnameshow,
Resource.Id.mnameshow
};
// creating a SimpleCursorAdapter to fill ListView object.
SimpleCursorAdapter scaTemp = new SimpleCursorAdapter(this, Resource.Layout.Main, icTemp, from, to); //Error on this line
lvTemp.Adapter = scaTemp;
}
else
{
text.Text = sql.Message;
}
}
your reference to list view should be
lvtemp=(ListView)findViewById(R.id.lst)