Reputation: 46
Good Morning, I tried getting the toast to show if the number of students is less than 0, or greater than 100. The app seems to work fine, except showing the toast. This assignment is due tomorrow night.
Here is my code:
package co.tekitall.classroommanager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import static co.tekitall.classroommanager.R.id.NumOfStudents;
public class Classroom extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classroom);
Button button = (Button) findViewById(R.id.SubmitClassRoomInfoButton);
}
void ClassroomElements() {
//ArrayList
ArrayList<EditText> arrayList = new ArrayList<>();
arrayList.add((EditText) findViewById(R.id.Teacher_Name));
arrayList.add((EditText) findViewById(R.id.Room_Number));
arrayList.add((EditText) findViewById(R.id.ClassroomHelper));
arrayList.add((EditText) findViewById(R.id.NumOfStudents));
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText numofstudents = (EditText) findViewById(R.id.NumOfStudents);
String getstucount = numofstudents.getText().toString();
int setstucount = Integer.parseInt(getstucount);
if(setstucount < 0) {
numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Please try Again! Number must be greater then 0 and less than 100...", Toast.LENGTH_LONG);
toast.show();
}
if(setstucount > 100) {
numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Please try Again! Number must be greater then 0 and less than 100...", Toast.LENGTH_LONG);
toast.show();
} else
numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Good Job",
Toast.LENGTH_LONG);
toast.show();
}
};
}
Upvotes: 0
Views: 55
Reputation: 37404
You need to bind button
and listener using
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classroom);
Button button = (Button) findViewById(R.id.SubmitClassRoomInfoButton);
button.setOnClickListener(listener);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Plus use if
and else-if
instead of multiple if
s because a number can only be either less than 0
otherwise greater than 100 or between|equals 0 or 100
if(setstucount < 0){//...code}
else if(setstucount > 100){//...code}
else {//...code}
Upvotes: 3
Reputation: 152827
Looks like you have a Button
and an OnClickListener
but you're never connecting them together. For example:
button.setOnClickListener(listener);
Upvotes: 0