Android: java.lang.NumberFormatException: Invalid int: "1.01" - android

Hi in my app i am reading the values from the database thru cursor and displaying in textview
my cursor contains the value 1.01 now i wanna display 101 in my text view..doing the following
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
int ach = Integer.parseInt(topcursor.getString(6));
tv2.setText(ach +"");
i am getting the float value as 1.01, now i wanna show the percentage in textview i.e, 101% .how can i do that
But iam getting numberformatexception. Any help is appreciated.

1.01 is not an integer value, that is why the conversion is failing. Also be careful with locale when you use the parse* methods

Try this
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
String ach = topcursor.getString(6);
tv2.setText(ach);

There is no need to parse as it returns the string.
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
String mResult = topcursor.getString(6);
tv2.setText(mResult);

Thank You all ...Did as follows it work fine..
float ach = Float.parseFloat(topcursor.getString(6));
String kj = String.valueOf(ach*100+"%");

Using replace() will do the trick
TextView tv2 = (TextView)view.findViewById(R.id.acValue);
int ach = Integer.parseInt(topcursor.getString(6).replace(".", ""));
tv2.setText(ach +"%");

Related

Comparing TextView's getText() w/ R.string.stringValue in android?

Here's my code that's giving me grief.
TextView questionView = (TextView) findViewById(R.id.questionView);
if(questionView.getText().equals(R.string.begginigStatement){
currentQuestionIndex = -2;
Log.d(TAG, "the TextView's text is equal to R.string.beggingStatement);
}
I'm trying to compare a string w/ an int
but I can't figure out the solution other than perhaps hardcoding the string, though I know that's not a proper convention. What's the solution?
R.string.begginigStatement is just an ID of the string as generated in R.class. To retrieve the value call:
getResources().getString(R.string.begginigStatement)
try to use:
context.getResources().getString(R.string.begginigStatement);
and context can be 'getActivity()' if it's in Fragment or just :
getResources().getString(R.string.begginigStatement)
if it has context
You have to compare this string values:
questionView.getText().toString().equal(getResources().getString(R.string.begginigStatement))

Display float value in TextView using SimpleAdapter

I am using Custom ListView which contains ProgressBar and TextView. Now, Progress Data may contain float value so I want to display float value in TextView. But It is showing error : can not parse '43.20' to int. If I am passing int then It is working fine.
How to prevent this problem ?
My Sample Code :
final SimpleAdapter adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic", "Accuracy", "Accuracy" },
new int[] { R.id.tvTopic, R.id.pbTopicAccuracy, R.id.tvAccuracy });
tvAccuracy is the TextView about which I am talking.
Log:
03-11 12:32:38.913: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytestbuddy.anatomy/com.mytestbuddy.anatomy.ReportCard}: java.lang.NumberFormatException: unable to parse '43.24' as integer
First u nee to convert floating values to string like this
String Str = String.valueOf("43.50");
Then assign str to textview.
TextView txt = new TextView(this);
txt.settext(Str);
like this,Try this one.
You can concat the float value
textView.setText(""+floatValue);
Use Math.round() before typecasting using (int) should round the float to the nearest whole number
Another way you can try this one also:
Float value = 30.0F;
Integer intValue=Integer.valueOf(value.intValue());
and then you can convert inValue to String format and then set String on Textview as you need.
Hope it will help you!!

How to use constant & int based string combindly?

My textview -
TextView doses = (TextView) findViewById(R.id.doses);
int getting code -
int nIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DOSES);
What I want is add some pre defined TEXT like "DOSE:" before nIndex.
My expected result will be like-
doses.setText(cursor.getString("DOSE"+nIndex));
How to do that??
Just use this:
doses.setText("DOSE" + cursor.getString(nIndex));

Android array of ids to reference textViews

I have a screen with 24 TextViews that I want to change values of. It doesn't matter in which order I just need to change the value from every text field. Right now my code is as follows:
textView = (TextView) findViewById(R.id.textView1);
textView.setText(value1);
textView = (TextView) findViewById(R.id.textView2);
textView.setText(value2);
textView = (TextView) findViewById(R.id.textView3);
textView.setText(value3);
textView = (TextView) findViewById(R.id.textView4);
textView.setText(value4);
etc...
The number of TextViews is constant at 24. I am looking for a loop solution however the issue I run into is dynamically getting the R.id.textViewX value id. Is there a simple way to accomplish this so that I can use it in the following manner:
// Somehow get textViewArray of textView id's //
for(int i=0;i<textViewArray.length;i++) {
textView = (TextView) findViewById(textViewArray[i]);
textView.setText(value[i])
}
I'm open to the idea of dynamically creating the textfields in the Activity class but am looking for an XML solution. I'm not sure if this can be done using the arrays class. I've seen this done for #drawables but never for id's
Important to note is that the textView's are NOT in a list.
Use the getIdentifier() method:
for (int i = 0; i < 24; i++) {
int id = getResources().getIdentifier("textView" + i, "id", getPackageName());
TextView textView = (TextView) findViewById(id);
textView.setText("Set text");
}

How to parse data under Textview

can anybody help me? I'm writing an application for Android that parses data from internet and writes them into TextView.
TextView[] dva = new TextView[sitesList.getDva().size()];
// Set the result text in textview and add it to layout
for (int i = 0; i < sitesList.getDva().size(); i++) {
dva[i].new TextView(this);
dva[i].setText("KOSILO: "+sitesList.getDva().get(i));
layout.addView(dva[i]);
Under dva[i] I parse 5 objects that I would like them to display them under one TextView.
TextView textView1 = (TextView) findViewById(R.id.kosilodva);
textView1.setText("KOSILO: "+sitesList.getDva().get(i));
How can I do this? Thanks in advance to everybody who is willing to help me.
Instead of setText why don't you try append,
Replace this,
textView1.setText("KOSILO: "+sitesList.getDva().get(i));
with,
textView1.append("KOSILO: "+sitesList.getDva().get(i));

Categories

Resources