I have a large table with all kinds of strings and numbers, and a number of them have to be editable. The table is loaded from an object with getters and setters of course.
I want to create a general input screen with an edit text and an ok button; and use that to edit the fields. However, i can't get this to work, this is a snippet of my code:
public class InputViewController extends Activity {
private String inputType;
EditText mEdit;
private String inputString;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inputscreen);
Bundle extras=getIntent().getExtras();
{
//get the type of input from the screen that sent us here, contained in a putextra
inputType = extras.getString("inputType");
TextView textViewInput = (TextView) findViewById(R.id.textViewInput);
textViewInput.setText(inputType);
mEdit = (EditText)findViewById(R.id.editText1);
}
//save input and return to previous screen
final Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//save input based on inputType
if(inputType.equals("Oplossing")){
inputString = mEdit.getText().toString();
ActivityViewController.activityObject.setSolution(inputString);
System.out.println(ActivityViewController.activityObject.getSolution());
}
//return to previous screen
finish();
}
});
Related
can i know pls why the input variable why it's giving me error always, thank you so much.
This is my code below:
public class MainActivity extends AppCompatActivity {
public TextView textView;
public EditText editText;
public Button btn;
public int input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText = findViewById(R.id.editText);
btn = findViewById(R.id.btn);
` input= Integer.parseInt(editText.getText().toString());`
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(input );
}
});
}
}
The error is happening as the String cant be cast to an integer. This could be because your editText is empty or that you have characters that are not integers in there (E.g. Letters). Therefor you should use try and catch to catch the error.
Currently you are trying to parse the input string from editText in onCreate() as soon as the view is created. This will try to parse the default text associated with the editText. I think what you want is to parse the input at the time of clicking button. In that case move the input parsing code inside onClick(). Also it is better to catch NumberFormatException when calling parseInt() if you cannot guarantee that the text entered in the EditText is always an integer.
#Override
public void onClick(View v) {
android.util.Log.i("MyActivity", "onClick: edit text = " + editText.getText());
input= Integer.parseInt(editText.getText().toString());
textView.setText(input );
}
Learning how to produce random numbers from arrays. The code will pick a random string during the onCreate method, but when the button is clicked there is no change to the string. Am I messing up the scope of my variables somehow? I really do not know what it wrong.
public class MainActivity extends AppCompatActivity {
String[] qArray; //initialize qArray
private static final Random rgen = new Random(); //sets the new Random method to the keyword rgen
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qArray = getResources().getStringArray(R.array.qArray); //grabs string array in the XML file
String q = qArray[rgen.nextInt(qArray.length)];//generates a random index of qArray to set to q
TextView question = (TextView) findViewById(R.id.question); //sets the TextView to "question"
question.setText(q); //sets the text in "question" to q
}
public void onClick(View view){
Button button = (Button) view.findViewById(R.id.button_click);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
/*This should do the same thing as onCreate but each time
the button is pressed. I think the error is here.*/
String q = qArray[rgen.nextInt(qArray.length)];
TextView question = (TextView) findViewById(R.id.question);
question.setText(q);
}
});
}
}
First of all you haven't set click listener on button properly you made onClick method which is never called . Also you are getting the references of view multiple times you should do it like this.
public class MainActivity extends AppCompatActivity {
String[] qArray; //initialize qArray
private static final Random rgen = new Random(); //sets the new Random method to the keyword rgen
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qArray = getResources().getStringArray(R.array.qArray); //grabs string array in the XML file
String q = qArray[rgen.nextInt(qArray.length)];//generates a random index of qArray to set to q
TextView question = (TextView) findViewById(R.id.question); //sets the TextView to "question"
question.setText(q); //sets the text in "question" to q
Button button = (Button) view.findViewById(R.id.button_click);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
/*This should do the same thing as onCreate but each time
the button is pressed. I think the error is here.*/
String q = qArray[rgen.nextInt(qArray.length)];
question.setText(q);
}
});
}
}
You either need to set the onClickListener in xml (in which case you don't need to set the listener in code), or you need to set the listener in onCreate. Right now its being set in a function named onClick, which is never called.
First, you never set the onClickListeren. You have a method which adds one, but you never call it. Move the code in the onClick method to your onCreate method. This will probably fix your problem.
If not, don't make your Random final and add this to your #Override onClick():
rgen = new Random(System.currentTimeMillis());
This will use a different seed every time
I want the fields entered by the user to be displayed as a list view of the studentnoText and courseField in a new activity..
public class Student extends Activity {
private String totalStudents [];
private EditText studentnoText;
private EditText studentnameText;
private EditText courseField;
private Spinner departmentSpinner;
private EditText dobField;
private Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
studentnoText = (EditText) findViewById(R.id.studentnoText);
studentnameText = (EditText) findViewById(R.id.studentnameText);
dobField = (EditText) findViewById(R.id.dobField);
courseField = (EditText) findViewById(R.id.courseField);
departmentSpinner = (Spinner) findViewById(R.id.departmentSpinner);
registerButton = (Button) findViewById(R.id.registerButton);
}
public void registerButtonPressed (View view){
Log.v("Register","Register Pressed!");
}
}
USE PUT EXTRA TO SEND DATA
mIntent.putExtra("TEXT", studentnoText.gettext().toString());
mIntent.putExtra("TEXT_1", studentName.gettext().toString());
USE GET EXTRA TO GET DATA
String strNO,strName;
strNO= extras.getString("TEXT");
strName = extras.getString("TEXT_1");
What I'm trying to do is:
If the EditText input is equal to the random number generated, then stop the loop otherwise keep on the loop and reset input text.
For some reason, I'm getting an infinite loop. I am new to programming, any help is really appreciated.
Here is the code:
public class Main extends Activity implements OnClickListener{
private TextView tvResult;
private TextView tvRandTest;
private EditText et1;
private String randonNumber;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvResult = (TextView) findViewById(R.id.textView4);
tvRandTest = (TextView) findViewById(R.id.textView3);
et1 = (EditText) findViewById(R.id.editText1);
}//End Main
public void myClickHandler(View view)
{
if(view.getId() == R.id.button1)
{
//Generates 6 one digit Random Numbers
int randonNumber1 = (int) (0 + Math.random() * 9);
//Parse Numbers
String rd1 = Integer.toString(randonNumber1);
randonNumber = rd1;
boolean done = false;
do
{
et1.getText().toString();
if(et1.equals(randonNumber))
{
Toast.makeText(Main.this,"Equal Number", Toast.LENGTH_SHORT).show();
tvResult.setText(randonNumber);
done = true;
}//end if
else
{
Toast.makeText(Main.this,"Not Equal Number", Toast.LENGTH_SHORT).show();
et1.setText("");
}//end else
}//End While
while(!done);
}//End if
if(view.getId() == R.id.button2)
{
tvRandTest.setText(randonNumber);
}
}//End Method
#Override
public void onClick(View arg0) {
// TODO
}
}//End Class
if(et1.equals(randonNumber))
I will change it with
if(et1.equals(String.valueOf(randonNumber)))
you put directly the int value inside the equals method two things will happen:
the autboxing will create an Integer object starting from the int value
the toString() method will be called through this object.
the toString() method of Integer in android, as the doc stands:
Returns a string containing a concise, human-readable description of this object.
So you are compareing the address of the new object with the content of et1 and not with its real value. Here the reference
In this line et1.getText().toString(); you need to assign result to variable, for example String input = et1.getText().toString(); Then in next line you need to compare two strings, if(input.equals(randonNumber)) But your program can hang cause of infinity loop on UI thread. You should use TextWatcher
to handle when text in EditText was changed
I am making an application in which i have to fetch item name and price from first activity to another and i am able to do that but in second activity i am allowing user to put quantity in numbers and whenever user will click on button a TextView to show total amount, but not able to calculate total Amount for item.
I know this is very simple task to do but i am getting error while write this line in button onClick():
txtResult=txtCost*txtQty
here i am placing second activity code,
please correct this one:-
public class SecondScreenActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView txtName = (TextView) findViewById(R.id.txtName);
TextView txtCost = (TextView) findViewById(R.id.txtCost);
EditText txtQty=(EditText)findViewById(R.id.txtQty);
Button btnClose = (Button) findViewById(R.id.btnCalculate);
TextView txtResult = (TextView) findViewById(R.id.txtResult);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String cost = i.getStringExtra("cost");
// Displaying Received data
txtName.setText(name);
txtCost.setText(cost);
// Binding Click event to Button
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
//finish();
txtResult=txtCost*txtQty;
}
});
}
}
You can do it by
int cost = Integer.parseInt(txtCost.getText().toString());
int qty = Integer.parseInt(txtQty.getText().toString());
int result = cost*qty;
and then set this result into txtResult
txtResult.setText(result+"");
or you can convert int to String and then apply setText
Why are to multiplying value in Textview
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Float f_name = new Float(name);
Float f_cost = new Float(cost);
Float f_result=f_name *f_cost ;
txtResult.setText(f_result);
}
});
}
}
txtResult=txtCost*txtQty;
These are TextView objects. You can't do maths on objects. Get the textview's values into integers and then try to multiply.
Do something like this.. depending on the type of variable you want (integer, float, double).
int a = new Integer(txtCost.getText().toString());
int b = new Integer(txtQty.getText().toString());
int c = a * b;
txtResult.setText(d);
There are 2 changes that needs to be made.
Make both txtCost and txtQty global variables.
both these variables cannot be directly multiplied. Instead do somthing like this.
double cost = Double.parseDouble(txtCost.getText().toString());
double qty = Double.parseDouble(txtQty.getText().toString());
txtResult = cost * qty ;