I am making a simple budget app and would like to add all inputted income then save this income to use in other classes. I'm lost and not sure how to do it. Here's the portion of my code inside of my onCreate method. I have income and incomeName both as Strings
addIncomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO: Transfer this info to line in scroll view showing incomes
if (TextUtils.isEmpty(enterIncomeEditText.getText()) |
TextUtils.isEmpty(enterIncomeNamesEditText.getText())) {
Toast.makeText(Income.this, "Entry Empty", Toast.LENGTH_SHORT).show();
} else {
//create income and incomeName strings
income = enterIncomeEditText.getText().toString();
incomeName = enterIncomeNamesEditText.getText().toString();
mLayout.addView( createNewTextView(incomeName + " " + income));
}
}
});
You can use Intent.
Assume you want to pass all inputted data to Activity B
Intent intent = new Intent(getApplication(), ActivityB.class);
intent.putExtra("income",income);
intent.putExtra("incomeName",incomeName);
startActivity(intent);
Then in Activity B, you can use getStringExtra() to get all inputted value.
Related
Hi Iam creating this app were you input Name and it automatically put the Time-inon the listview with current time. Now, if the user were to put the same Name, the system then recognized it to avoid duplication.
Here is the code for condition
getTime();
String fnlAddName = finalTextName+"\n"+formattedDate+"\n"+strTime;
if (names.indexOf(finalTextName) > -1){
//the system recognized the same input
String beforeName = listView.getItemAtPosition(position).toString();
names.add(beforeName+"\n"+strTime);
myAdapter.notifyDataSetChanged();
}else{
names.add(fnlAddName);
myAdapter.notifyDataSetChanged();
dialog.cancel();
position = position+1;
}
Now, I already achieved to detect same input from the user. What I want now is, I want to take that same data from the list (with also the time) and add another current time. So the list must update from "Name+1stCurrentTime" to "Name+1stCurrentTime+2ndCurrentTime"
Your code should look something like this
if (names.indexOf(finalTextName) > -1){
//the system recognized the same input
int index = names.indexOf(finalTextName);
names.set(index, names.get(index) + "\n" + strTime);
myAdapter.notifyDataSetChanged();
}else{
names.add(fnlAddName);
myAdapter.notifyDataSetChanged();
dialog.cancel();
position = position+1;
}
With the indexOf method you can get the position of the list that contains the name, then we replace it, I hope this helps you.
I am practicing to build a calc app in Android Studio using Java. here's how it looks like
Yes it is very simple
Now instead of using buttons to enter numbers, I used two EditText views for entering numbers. now I wrote a method to add two numbers like this:
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int result;
first=Integer.valueOf(input1.getText().toString());
second=Integer.valueOf(input2.getText().toString());
result = first+second;
value.setText(""+result);
Toast.makeText(MainActivity.this, "Please fill the both space with numbers", Toast.LENGTH_LONG).show();
}
the code for adding numbers is fine but if I do not have an input in EditText views, the app crashes. I want to put the codes in an if else clause. if there are inputs in both spaces app does the operation but if not it makes a toast and askes for numbers from the user. but I have no idea how to code the if else conditions. anybody can help me?
You can test if the input1 or input2 is empty as your if else testing criteria. The following code assume you have correctly found and initialized input1 and input2 editText views.
#Override
public void onClick(View v) {
int result;
Editable firstInputText = input1.getText();
Editable secondInputText = input2.getText();
if(firstInputText != null && firstInputText.length>0 &&
secondInputText != null && secondInputText.length>0){
first=Integer.valueOf(firstInputText.toString());
second=Integer.valueOf(secondInputText.toString());
result = first+second;
value.setText(""+result);
} else{
Toast.makeText(MainActivity.this, "Please fill the both space with numbers", Toast.LENGTH_LONG).show();
}
}
Also for your calculator, you want to ensure the inputs are numeric strings. For your layout xml, you can specify it as follows.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_test_editText"
android:inputType="number"
/>
I have a problem with adding values to list which is in SecondActivity. In MainActivity I set text in EditText boxes and send to second class. For the first time values are adding, but when I back to previous activity and one more time set text and send it, values in the list are replaced, not added. Someone know what is the source of this problem?
try this in first activity:
String[] array = {"Hi", "there", "yeah"};
Intent goIntent = new Intent(this, NewAppActivity.class);
/*
* put extra with "array" as a key and the String[] with your values as the value to pass
* */
goIntent.putExtra("array", array);
startActivity(goIntent);
and in second activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String[] array = extras.getStringArray("array");
}
I am trying to implement a button that saves integers entered into an EditText and save them into an ArrayList. I declared my ArrayList globally in my class and am calling it inside of my OnClickListener method. I am unsure whether or not I am saving to this ArrayList because I am unable to display what I have saved in said ArrayList.
My declaration of the list is;
ArrayList<String> savedScores = new ArrayList<String >();
This is what I am using to save to my ArrayList;
`savedScores.add(input1.getText().toString());`
Now, in my OnClickListener method, I have a button that saves user input into the ArrayList (or so I am hoping), and another to display what I have saved. However, when I click on the "editScore" button, the TextEdit is cleared as if I have nothing saved in my ArrayList. This is simply a test to see if I am properly saving to my array and any help would be much appreciated! Thank you.
switch (view.getId()) {
case R.id.buttTotal:
if (blankCheck.equals("")) {
Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
blankError.show();
break;
} else {
int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
int sum = num1 + score2;
score2 = sum;
output1.setText("Your score is : " + Integer.toString(sum));
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
break;
}
case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
output1.setText("you messed up");
break;
case R.id.editScore: //Need to set up Save Array before we can edit
output1.setText(savedScores.get(0));
break;
}
Because you are saving empty values into your ArrayList. See here
input1.setText(""); //Clear input text box
//SAVE TO THE ARRAYLIST HERE
savedScores.add(input1.getText().toString());
The value of input1 is empty. Clear the input after you saved it to the array.
I would like to change a TextView as a user adds things to a cart. The initial value is 0.00 and as the user adds items, this is added to the value. I have an AlertDialog that pops up when clicking a button that allows the user to choose an item.
My issue is a java.lang.StringToReal.invalidReal error. I think that I may not be getting the value of the TextVeiw properly but am not totally sure.
Thanks to anyone looking at this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.pickItem);
builder.setItems(R.array.items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
CartItems ci = new CartItems();
ci.setItem(which);
ci.setPrice(which);
cart.add(ci);
totalPriceTV = (TextView)findViewById(R.id.textView2);
double totalPrice = Double.parseDouble(totalPriceTV.toString());
totalPrice += ci.getPrice();
String newTotal = new Double(totalPrice).toString();
totalPriceTV.setText(newTotal);
}
});
builder.create();
builder.show();
}
});
}
In this line
double totalPrice = Double.parseDouble(totalPriceTV.toString());
Change totalPriceTV.toString() to totalPriceTV.getText().toString() and try again.
In order to change the text of a TextView in Android just use the ordinary geters and setters:
TextView.getText();
TextView.setText("text");
Since you deal with numbers i suggest you to use DecimalFormat when parsing a double to string. You can easily define the format of the number (i.e the number of digits after comma or the separator characters)
DecimalFormat df = new DecimalFormat("###,###.00");
String price = df.parse(someDouble);
textView.setText(price);
For these numbers: 234123.2341234 12.341123 the DecimalFormat would give you the following result:
234,123.23 and 12.34
Taken from page: http://developer.android.com/reference/android/view/View.html#toString()
Added in API level 1
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
getClass().getName() + '#' + Integer.toHexString(hashCode())
As a result you should use getText();
ie:
totalPriceTV.getText()
Just use totalPriceTV.setText(""+totalPrice);
or
totalPriceTV.setText(String.valueOf(totalPrice));