Reputation: 53
I am trying to display list of results in the same Text View.
Below is my code... this code works but only displays the last value of the for loop.I need the code to print the following.
The result is : 2 * 0 = 0
The result is : 2 * 1 = 2
The result is : 2 * 2 = 4
The result is : 2 * 3 = 6
The result is : 2 * 4 = 8
The result is : 2 * 5 = 10
The result is : 2 * 6 = 12
The result is : 2 * 7 = 14
The result is : 2 * 8 = 16
The result is : 2 * 9 = 18
Kindly advice. Thanks in advance.
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
}
}
Upvotes: 0
Views: 2002
Reputation: 2897
You can use a String
variable to display all the results by using concat
function and after the loop is executed just set this string
to your textView
like:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat("The result is : " + userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
You can also use "The result is" only one time before the loop and display the result like:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
answerString = "The result is : \n";
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat(userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
and your output will be like:
The result is :
2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
Upvotes: 2
Reputation: 409
package com.example.count;
import java.util.Scanner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView tv;
int input=2;
int result;
String finalresult="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.txt);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String res= rint();
tv.setText(res);
}
});
}
public String rint(){
for (int i = 0; i < 10; i++) {
result = input * i;
finalresult=finalresult+"\n"+"The result is : " +input + " * " + i + " = " + result;
}
return finalresult;
}
}
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="14dp"
android:inputType="textMultiLine"
android:text="@string/hello_world" />
</RelativeLayout>
Upvotes: 1
Reputation: 1224
You can use append
instead of setText
example:
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
Or
You can take String variable use +=
to concatenate the string value and at last after the loop setText
to the TextView
example:
String strMultiplication = ""; // declare string before for loop
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
strMultiplication += "The result is : " + userInput + " * " + i + " = " + result +"\n"); //String concatenation
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
ans.setText(strMultiplication); // setText To the textView outside the loop
Upvotes: 1
Reputation: 35549
replace
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
with
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
and your problem is solved
Upvotes: 1
Reputation: 1413
In textview , It will so only the last set the text in textview.
set the text , while i=0 and then next append the text in textview after the for loop increment.
after that you will get the desire result.
if(i==0)
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
else
ans.append("\nThe result is : " + userInput + " * " + i + " = " + result);
Upvotes: 1