How to solve : number Format Exception Invalid int:"" - android

I am beginner in android app development. My code is as follows- How to solve Number Format Exception in this code-
public class InterestActivity extends AppCompatActivity {
private EditText editAmount;
private EditText editInterest;
private EditText editDuration;
private TextView resultView;
private Button Calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interest);
editAmount = (EditText) findViewById(R.id.editAmount);
editInterest = (EditText) findViewById(R.id.editInterest);
editDuration = (EditText) findViewById(R.id.editDuration);
resultView = (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
}
public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = Integer.parseInt(editAmount.getText().toString());
int i = Integer.parseInt(editInterest.getText().toString());
int d = Integer.parseInt(editDuration.getText().toString());
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}

public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = 0;
int i = 0;
int d = 0;
try{
a= Integer.parseInt(editAmount.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editAmount"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
i = Integer.parseInt(editInterest.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editInterest"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
d = Integer.parseInt(editDuration.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editDuration"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}

You are assigning the Views to temporary local variables instead of the attributes of the class.
Short solution:
Remove EditText before editAmount = ...
Likewise with all the other assignments inside onCreate.
If you want to continue Android programming you should understand why this will work. This might help you: What is the difference between field, variable, attribute, and property in Java POJOs?

Related

i want the value of inflated edittext with other layout using for loop in java

I need the value of inflated editText, I am inflating the design layout which is the child layout and MainActivity is a parent layout.
below is the MainActivity class
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is total code of store object and retrieval,
read all comments please,
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
// list of edittext
List<EditText> edtlist = new ArrayList()<EditText>;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
// store object in list
edtlist.add(edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
// call this whenever you want in any event
// here 0 is first edittext
// you can get all text value using for loop
// list.get(0).getText().toString();
}
}
You are looking for something like ,I'm not sure about it maybe it works maybe it wont.! it just a rough concept based code havnt Implemented yet.!
List<EditText> edtxtList;
List<String> edtxtStrList;
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
edtxtList.add(edittext);// try something like this.!
}
Then use Foreach loop on EditText;
for (EditText edText : edtxtList) {
if(!edText.getText().trim()=null){
edtxtStrList.add(edText.getText().trim());
}
}
I havnt tried It yet but maybe this ruff concept may helps you.!
Maybe this Method helps You.!
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
In Above code mLayout is your layout.!
<LinearLayout android:id="#+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>

how to get id from list view and send to other activity

i want when click on User info list view item (fill with adapter) , get user_id,
i use this code in main activity :
// long click on listview items
LIST_USER.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Clicked item id", " "+ id);
// Intent intent = new Intent(getBaseContext(), UpdateActivity.class);
// intent.putExtra("EXTRA_ID", id);
// startActivity(intent);
return true;
}
});
yet no problem !
but when send user_id to update activity database has stopped !!
use this code in update activity :
public class UpdateActivity extends AppCompatActivity {
private DatabaseHelper DB_HELPER;
private TextView TXT_VIEW;
private EditText EDT_NAME;
private EditText EDT_AGE;
private EditText EDT_GENDER;
private EditText EDT_PASS;
private EditText EDT_DESC;
private EditText EDT_PIC;
private Button BTN_UPDATE;
private Button BTN_DELETE;
private String STR_ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
TXT_VIEW = (TextView) findViewById(R.id.textView);
EDT_NAME = (EditText) findViewById(R.id.editText);
EDT_AGE = (EditText) findViewById(R.id.editText2);
EDT_GENDER = (EditText) findViewById(R.id.editText3);
EDT_PASS = (EditText) findViewById(R.id.editText4);
EDT_DESC = (EditText) findViewById(R.id.editText5);
EDT_PIC = (EditText) findViewById(R.id.editText6);
BTN_UPDATE = (Button) findViewById(R.id.button) ;
BTN_DELETE = (Button) findViewById(R.id.button2);
DB_HELPER = new DatabaseHelper(this);
STR_ID = getIntent().getStringExtra("EXTRA_ID");
TXT_VIEW.setText(STR_ID);
display user_id in log info :
thank's
The starting problem is in the types, you are passing a long and reading it as a string, the cast exception is raised in the onCreate and resulting in the crash. This should make it work:
private long STR_ID;
//...
STR_ID = getIntent().getLongExtra("EXTRA_ID");
TXT_VIEW.setText(String.valueOf(STR_ID));

How to make decision in onClick

This is for searching the word on the TextView
public void viewWord(View view)
{
score = (TextView)findViewById(R.id.yourScore);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
searchWord = (ImageView) findViewById(R.id.searchWord);
wordList = (ListView) findViewById(R.id.wordList);
text = (AutoCompleteTextView) findViewById(R.id.textHere);
wordList = (ListView) findViewById(R.id.wordList);
//SearchWord in AutoCompleteTextView text
//Display equivalent score of word in TextView score
String s1= search.getText().toString();
String s2= dbHelper.getData(s1);
score.setText(s2);
tv2.setText(s2);
tv3.setText(s2);
//Get data from textview
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
wordList.setAdapter(adapter);
adapter.add(text.getText().toString());
adapter.notifyDataSetChanged();
text.setText("");
add();
}
public void add (){
x = Integer.parseInt(tv2.getText().toString());
y = Integer.parseInt(tv3.getText().toString());
z = x + y;
score.setText(Double.toString(z));
}
In my app, I am using an ImageView to search the word and display the score. I am having a hard time in displaying the score using 1 TextView so I decided to make it 2. I want my ImageView to function like this:
if(onMyImageViewFirstClick){
display the score on textview1
}
else{
if(onMyImageViewSecondClick){
display the score on textview2
}
how can i do it this way? Any suggestions will be highly appreciated :)
Note: I am new in android programming. I would appreciate if you'll also help me with the codes
You can do it with a class variable changin his value between 0 - 1, for example:
private int optionTxtView = 0
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(optionTxtView == 0){
//display the score on textview1
optionTxtView = 1;
}else{
//display the score on textview2
optionTxtView = 0;
}
}
});
or you can use a boolean value (suggested by #mapo)
private boolean optionTxtView;
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(optionTxtView){
//display the score on textview1
optionTxtView = false;
}else{
//display the score on textview2
optionTxtView = true;
}
}
});
So, according to your code, the solution must be:
//declare a boolean variable
private int optionTxtView = 0
public void viewWord(View view) {
...
...
...
//SearchWord in AutoCompleteTextView text
//Display equivalent score of word in TextView score
String s1= search.getText().toString();
String s2= dbHelper.getData(s1);
score.setText(s2);
if(optionTxtView == 0){
//display the score on textview1
tv2.setText(s2);
optionTxtView = 1;
}else{
//display the score on textview2
tv3.setText(s2);
optionTxtView = 0;
}
...
...
...
}
Here's how you can implement a click counter.
Note how you reset the counter when a total of 2 clicks are registered. Once that happens the counter starts over and again will first affect tv1 and then tv2.
// Find TextViews before (so you don't have to repeat this expensive operation)
final TextView tv1 = findViewById(R.id.textView1);
final TextView tv2 = findViewById(R.id.textView2);
imageButton.setOnClickListener(new View.OnClickListener() {
int numberOfClicks = 0;
#Override
public void onClick(View view) {
++numberOfClicks;
if(numberOfClicks < 2){
tv1.setText("Some text");
} else if(numberOfClicks >= 2){
tv1.setText("Some other text");
// Reset the click counter
numberOfClicks = 0;
}
}
});

Total value of multiple textviews into a single textiew?

I used the answer to this question to get the basic value of the TextView clicks into a single TextView .This code gives a per click value, I want to know how to add the existing value of TextView.
Note: m1aa and m1ab are number variables in a previous activity and received through intent on this one.
UPDATE This question should have been "how do you convert a string to a decimal."
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***UPDATE***WORKS FINE :)
AFTER GETTING HELP AND "THINKING" ABOUT WHAT I WAS WRITING I END UP WITH...
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);
You can use following code to get a value of TextView and convert it to integer
String s = textView.getText().toString();
int n = Integer.parseInt(s);
Remember NumberFormatException will be thrown if the string does not contain a parsable integer.

Concatenation of EditText

in my Android application,
I have 4 EditText where the user puts his information
And I want to create an other EditText, where all information in the 4 EditText are concatenated... But I really don't arrive to do that ....
Here is my code :
private Editable mtext1 = null;
private Editable mtext2 = null;
private Editable mtext3 = null;
private Editable mtext4 = null;
EditText mtext5;
PendingIntent mNfcPendingIntent;
IntentFilter[] mWriteTagFilters;
IntentFilter[] mNdefExchangeFilters;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
setContentView(R.layout.main);
findViewById(R.id.write_tag).setOnClickListener(mTagWriter);
//final Button quitBut = (Button) findViewById(R.id.button1);
mNoteBarCode = ((EditText) findViewById(R.id.noteBareCode));
mNoteAirport = ((EditText) findViewById(R.id.noteAirport));
mNoteFlightNumber = ((EditText) findViewById(R.id.noteFlightNumber));
mNoteName = ((EditText) findViewById(R.id.noteName));
mNote = ((EditText) findViewById(R.id.noteFull));
mtext1 = mNoteBarCode.getText();
mtext2 = mNoteAirport.getText();
mtext3 = mNoteFlightNumber.getText();
mtext4 = mNoteName.getText();
mtext5 = (EditText) mtext1 + (EditText) mtext2 + .......; // doesn't work
mtext5 = (EditText) mNote.getText();
String firstString = firstET.getText().toString(); // get string from first EditText
String secondString = secondET.getText().toString(); // get string from second EditText
EditText concatenatedET = (EditText) findViewById(R.id.your_edittext); //declare EditText you want them put in together
concatentatedET.setText(firstString + secondString); // put the string values in
Let me know if this works for you or if you need a better explanation

Categories

Resources