How to add numbers together using and If statement? - android-studio

I'm trying to make a program in android studio that when sellected a higher education it automatically adds 200$ to the pay that was previously entered. But instead of getting 500+200=700, I get 500200. I ran trough the code numerous times and just can't seem to find the error. I'm still new on Java. This is the second window that appears when submitting the information, It shows all the info entered in the first window, but for some reason it can't 200 to the entered pay
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultatas);
Intent duom = getIntent();
String pavarde = duom.getStringExtra("pavarde");
String issilavinimas = duom.getStringExtra("issilavinimas");
int pay = duom.getIntExtra("atlyginimas", 0);
String laikas = duom.getStringExtra("laikas");
TextView rez = findViewById(R.id.rezultatas);
rez.setText("Pavardė: "+pavarde+"\n");
rez.append("Išsilavinimas: "+issilavinimas+"\n");
if(issilavinimas.equals("Aukštasis")){
rez.append("Gaunamas atlyginimas: " +pay+ 200 + "\n");
} else{
rez.append("Gaunamas atlyginimas: "+pay+ "\n");
}
rez.append("Keliasi: "+laikas+"\n");
}
This is the first window where all the information is entered:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void saugoti(View v){
EditText txt = findViewById(R.id.lastName);
String pavarde = txt.getText().toString();
Spinner issilavinimas = findViewById(R.id.issilavinimas);
String issilav = issilavinimas.getSelectedItem().toString();
EditText atl = findViewById(R.id.pay);
int pay = Integer.parseInt(atl.getText().toString());
TextView txt2 = findViewById(R.id.time);
String laikas = txt2.getText().toString();
Intent duom = new Intent(this, rezultatas.class);
duom.putExtra("pavarde", pavarde);
duom.putExtra("issilavinimas", issilav);
duom.putExtra("laikas", laikas);
duom.putExtra("atlyginimas", pay);
startActivity(duom);
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}

Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultatas);
Intent duom = getIntent();
String pavarde = duom.getStringExtra("pavarde");
String issilavinimas = duom.getStringExtra("issilavinimas");
int pay = duom.getIntExtra("atlyginimas", 0);
int total=pay+ 200;
String laikas = duom.getStringExtra("laikas");
TextView rez = findViewById(R.id.rezultatas);
rez.setText("Pavardė: "+pavarde+"\n");
rez.append("Išsilavinimas: "+issilavinimas+"\n");
if(issilavinimas.equals("Aukštasis")){
rez.append("Gaunamas atlyginimas: " + total + "\n");
} else{
rez.append("Gaunamas atlyginimas: "+pay+ "\n");
}
rez.append("Keliasi: "+laikas+"\n");
}
when you + with String concatination is actually working there instead of addition

Related

I need help converting a string to an integer

Good day,
I have an array adapter in Activity1 in my AndroidStudio project. I would like to transfer two specific items from the ArrayAdapter (in positions 1 and 2) to Activity2 with an intent. (the ArrayAdapter is connected to an editTextNumber) That works well if I then display the two items in a TextView. But if I want to convert the items to an int, the app always crashes. I can somehow just convert "message1" into an int. Why is that so?
Thank you for your help
(Activity 1):
[...]
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayAdapter adapterzeit = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, zeit1);
Intent intent = new Intent(this, Activity2.class);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent.putExtra("EXTRA_MESSAGE1", adapterzeit.getItem(1).toString());
intent.putExtra("EXTRA_MESSAGE2", adapterzeit.getItem(3).toString());
startActivity(intent);
}
});
}
(Activity 2):
[...]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
Intent intent = getIntent();
String message1 = intent.getStringExtra("EXTRA_MESSAGE1");
String message2 = intent.getStringExtra("EXTRA_MESSAGE2");
int numberInt = Integer.parseInt(message1);
int number2 = Integer.parseInt(message2);
}
}
You add the extra to the Intent like this:
intent.putExtra("EXTRA_MESSAGE1", adapterzeit.getItem(1).toString());
and then you try to extract it from the Intent like this:
String message1 = intent.getStringExtra("EXTRA_MESSAGE");
You are using different String values for the keys! EXTRA_MESSAGE != EXTRA_MESSAGE1
This is one reason why you should always use constant definitions for these kinds of things, then you wouldn't have this problem. For example, in Activity1:
public static final String EXTRA_MESSAGE1 = "EXTRA_MESSAGE1";
intent.putExtra(EXTRA_MESSAGE1, adapterzeit.getItem(1).toString());
and then in Activity2:
String message1 = intent.getStringExtra(Activity1.EXTRA_MESSAGE1);

i dont know why but it crashing me at startActivity(StartIntent); i try to sand a ArrayList of the class User to ScenedActivity

the cod crash on startActivity(StartIntent);
i dont know why i start us android studio and i try code that i had in my head
and i dont know why but it crashing me at startActivity(StartIntent); i try to sand a ArrayList of the class User to ScenedActivity
ArrayList<User> users = new ArrayList<>();
User Dan = new User("dan","ali","game12",1234);
static int cod;
final int num = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
users.add(Dan);
setContentView(R.layout.activity_main);
final EditText Cod = findViewById(R.id.Cod);
Button NextActivity = findViewById(R.id.NextActivity);
NextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isEmpty(Cod))//if the text box is empty pot 0 in cod
cod = Integer.parseInt(Cod.getText().toString());
else if (isEmpty(Cod))
cod = 0;
if (cod == num ) {// if the cod that pot in EditText = my cod(num) go on
Intent StartIntent = new Intent(getApplicationContext(), ScenedActivity.class);
StartIntent.putExtra("android.intent.category.LAUNCHER", "Hi");//sand to scened activity string "hi"
StartIntent.putExtra("android.intent.category.LAUNCHER1",users);//sand to scened activity ArrayList of User
startActivity(StartIntent);
}
else {
Toast.makeText(getBaseContext(),"Error worng code",Toast.LENGTH_SHORT).show();//sand
}
}
});
}
private boolean isEmpty(EditText etText) {//check if text box is empty
if (etText.getText().toString().trim().length() > 0)
return false;
return true;
}
}

TextWatcher in Android not setting values in other edittexts

I have three edittext values for which i am using textwatcher to update data.
I want that if i enter any value in anyone of the edit text then according to that value it should calculate and then update the edittext in which i entered the value automatically and after that set the value in other specif edit text and save the final value in string if need to send to any server.
//My Code
public class Land_details extends AppCompatActivity {
EditText land_acre_edit;
EditText land_kanal_edit;
EditText land_marla_edit;
Button land_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_land_details);
land_acre_edit = findViewById(R.id.land_acre_edit);
land_kanal_edit = findViewById(R.id.land_kanal_edit);
land_marla_edit = findViewById(R.id.land_marla_edit);
land_button = findViewById(R.id.save_land_detail);
land_acre_edit.addTextChangedListener(mytext_watcher);
land_marla_edit.addTextChangedListener(mytext_watcher);
land_kanal_edit.addTextChangedListener(mytext_watcher);
}
private TextWatcher mytext_watcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String check_marla = land_marla_edit.getText().toString();
String check_kanal = land_kanal_edit.getText().toString();
String check_acre = land_acre_edit.getText().toString();
if (check_marla != null) {
if ((Integer.valueOf(check_marla) > 8)) {
check_acre =(String.valueOf(Integer.valueOf(check_kanal) + (Integer.valueOf(land_marla) / 20)));
check_marla=( String.valueOf(Integer.valueOf(land_marla) % 20));
check_acre = String.valueOf(check_acre) + (Integer.valueOf(check_kanal) / 8);
check_kanal = String.valueOf(Integer.valueOf(check_kanal) % 8);
land_marla_edit.setText(check_marla);
land_kanal_edit.setText(check_kanal);
land_acre_edit.setText(check_acre);
}}
}catch (NumberFormatException e){
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I have tried to set my try catch code in aftertextChanged but at first it was giving me numberformatexception as soon as i enter a text above my contion my app crash but after that i tried using try catch to catch a numberformatexception after that i am able to add number above my condition and even after a long wait nothing happen can any one explain where i am going wrong.

Why isn't my ImageView link displaying via Intent?

I'm loading an Image into an ImageView using a link, and I'm trying to pass this link from one activity to display on another activity by an Intent. However, the Image just seems to come up blank.
Below I have listed the code associated with the two activities, as well as the adapter of the RecyclerView that the information is being passed from in the first activity
ActivityOne.java
private ArrayList<String> mImageUrls = new ArrayList<>();
...
mImageUrls.add("https://data.oireachtas.ie/ie/oireachtas/member/id/Se%C3%A1n-Haughey.S.1987-04-25/image/large");
...
#Override
public void onNoteClick(int position) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("EXTRA_KEY_NAME", mNames.get(position));
intent.putExtra("EXTRA_KEY_IMAGE", mImageUrls.get(position));
startActivity(intent);
}
ActivityTwo.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_members_map_detailed);
tvMemberName = findViewById(R.id.tvClareMemberNameDetailed);
tvMemberParty = findViewById(R.id.tvClareMemberPartyDetailed);
tvMemberConstituency = findViewById(R.id.tvClareMemberConstituencyDetailed);
tvMemberAddress = findViewById(R.id.tvClareMemberAddressDetailed);
tvMemberEmail = findViewById(R.id.tvClareMemberEmailDetailed);
tvMemberPhone = findViewById(R.id.tvClareMemberPhoneDetailed);
tvMemberContributions = findViewById(R.id.tvClareMemberContributionsDetailed);
imgMember = findViewById(R.id.imgClareMemberImageDetailed);
Intent intent = getIntent();
String mName = intent.getStringExtra("EXTRA_KEY_NAME");
String mParty = intent.getStringExtra("EXTRA_KEY_PARTY");
String mConstituency = intent.getStringExtra("EXTRA_KEY_CONSTITUENCY");
String mAddress = intent.getStringExtra("EXTRA_KEY_ADDRESS");
Uri myUri = Uri.parse(intent.getStringExtra("EXTRA_KEY_IMAGE"));
String mEmail = intent.getStringExtra("EXTRA_KEY_EMAIL");
String mPhone = intent.getStringExtra("EXTRA_KEY_PHONE");
String mContributions = intent.getStringExtra("EXTRA_KEY_CONTRIBUTIONS");
tvMemberName.setText(mName);
tvMemberParty.setText(mParty);
tvMemberConstituency.setText(mConstituency);
tvMemberAddress.setText(mAddress);
tvMemberEmail.setText(mEmail);
tvMemberPhone.setText(mPhone);
tvMemberContributions.setText(mContributions);
imgMember.setImageURI(myUri);
Adapter information associated with Image
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Glide.with(mContext)
.asBitmap()
.load(mImageUrls.get(position))
.into(holder.image);
holder.name.setText(mNames.get(position));
holder.party.setText(mParties.get(position));

Non-static edit() cannot be referenced from a static context

Good evening. Or whatever time you're reading this in. In Android Studio when I was developing the first stages of my app I ran into a problem and I can't seem to find the answer. I am trying to use SharedPrefereces to save user input, but when I try to edit the prefereces [editor = SharedPreferences.edit();] it says that edit() is a non-static method and connot be refereced from a static context.
Here is the code.
public class TheButton extends AppCompatActivity {
EditText ed1, ed2, ed3, ed4;
/*EditText nameInput;
EditText ageInput;
EditText occupationInput;
EditText genderInput;
Button startButtonFinish;*/
protected static final String MyPREFERENCES = "";
public static final String nameInput = "";
public static final int ageInput = 0;
public static final String occupationInput = "";
public static final String genderInput = "";
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_button);
ed1 = (EditText) findViewById(R.id.nameInput);
ed2 = (EditText) findViewById(R.id.ageInput);
ed3 = (EditText) findViewById(R.id.occupationInput);
ed4 = (EditText) findViewById(R.id.genderInput);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Button startButtonFinish = (Button) findViewById(R.id.startButtonFinish);
startButtonFinish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nL = ed1.getText().toString();
String oI = ed3.getText().toString();
String gI = ed4.getText().toString();
//Gives me an error on edit() saying that it edit() is a non-static
// class and they cannot be referenced from a static context
editor = SharedPreferences.edit();
editor.putString(nameInput,nL);
editor.putString(occupationInput, oI);
editor.putString(genderInput, gI);
editor.commit();
startActivity(new Intent(TheButton.this, thebutton2.class));
}
});
}
I will appreciate any help. Thank you!
Instead of this line editor = SharedPreferences.edit();
you should call edit method by sharedpreferences object which is initialized in your code.
You should call the .edit() method by creating object of SharedPreferences. Change SharedPreferences in line, from editor = SharedPreferences.edit(); to editor = sharedPreferences.edit();
EditText ed1, ed2, ed3, ed4;
/*EditText nameInput;
EditText ageInput;
EditText occupationInput;
EditText genderInput;
Button startButtonFinish;*/
protected static final String MyPREFERENCES = "";
public static final String nameInput = "";
public static final int ageInput = 0;
public static final String occupationInput = "";
public static final String genderInput = "";
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_button);
ed1 = (EditText) findViewById(R.id.nameInput);
ed2 = (EditText) findViewById(R.id.ageInput);
ed3 = (EditText) findViewById(R.id.occupationInput);
ed4 = (EditText) findViewById(R.id.genderInput);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Button startButtonFinish = (Button) findViewById(R.id.startButtonFinish);
startButtonFinish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nL = ed1.getText().toString();
String oI = ed3.getText().toString();
String gI = ed4.getText().toString();
//Gives me an error on edit() saying that it edit() is a non-static
// class and they cannot be referenced from a static context
editor = sharedPreferences.edit();
editor.putString(nameInput,nL);
editor.putString(occupationInput, oI);
editor.putString(genderInput, gI);
editor.commit();
startActivity(new Intent(TheButton.this, thebutton2.class));
}
});
}

Resources