Display float value in TextView using SimpleAdapter - android

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!!

Related

How to Format double data from database and set into Textview using comma as decimal divider on Android

I have field on table as double (money data field), and i want to put it on Textview in format for example:
money= 20.0
i want to show as: 20,0
or money = 190.25
i want to show as: 190,25
how to do that? this is my variable
txtTotalBill.setText(String.valueOf(total_bill));
You can use the String method replace to format your chain :
txtTotalBill.setText(String.valueOf(total_bill).replace('.', ','));
String total = String.valueOf(total_bill);
total = total.replace(".", ",");
try this and then settext hope it helps
txtTotalBill.setText(total.toString());
Or you can directly setText without variable like this:
txtTotalBill.setText(String.valueOf(total_bill).replace('.', ','));

How to Show data from String Array ,in Single Android textview with cross image Each line contains Two Strings

1.I am getting data from server storing inside string Array.
2.then i want to show in Android TextView ,each TextView line should Contain 2 strings(Like Skill Sets) from String array.
3.then need to add cross Image in right side of the Textview.
please help me, how to achieve this.
String mystring = "This is my first sentence";
String arr[] = mystring.split(" ", 3);
String firstWord = arr[0]; //This
String secondWord = arr[1]; //is
String theRest = arr[2]; //my first sentence
yourtextview.setText(firstWord +" "+ secondWord);
and regarding for Image in right side of the Textview put this in ur txtview'sxml.
android:drawableRight="#drawable/image"

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

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 +"%");

How to assign values from an array to textviews in android

I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);

Handle drawables - by the name

I already know how to handle drawables like this:
final int[] imgSizeIds = new int[]{
R.drawable.zero,R.drawable.one,R.drawable.two,
...
};
and
setImageResource(imgSizeIds [ size ] );
in android. But what If I have an Array of Strings that is populated like this:
String[] names = new String[]{"oliver", "peter", "mike"};
and I would like to use Image oliver.jpg when The ROW Oliver is going to be handled.
Something like:
setImageResource(R.drawable. (AND HERE COMES "oliver") );
So the result will be:
setImageResource(R.drawable.oliver);
Two Arrays would not be the big problem but I also do not know how to put a string into the integer Array of drawables.. I cannot do something like:
final int[10] imgSizeIds;
String stringy = "oliver";
int[0] = "R.drawable." + stringy;
So that the 1st item in that array would be set to R.drawable.oliver
try this
int resid = getResources.getIdentifier("oliver" , "drawable", getPackageName());
setImageResource(resid);
setImageResource(R.drawable. (AND HERE COMES "oliver") );
So the result will be: setImageResource(R.drawable.oliver);
this will probably not happen.Because when you tell android to take a string specified in a String variable,it would take it as - "oliver" and put it like R.drawable."oliver" or even though,if you would try putting it like - setImageResource("R.drawable."+ (AND HERE COMES "oliver") ) it will take it as a String like "R.drawable.oliver" which is after all a String variable and not appropriate to be passed in setImageResource() method.
Be sure that R.drawable.imagename gives you int, not a string.
See if below code helps-
String[] imgNames = new String[]{ "oliver", "apple", "car"};
and use this to use names from name string array-
ImageView imgvw = (ImageView) findViewById(getResources().getIdentifier(imgNames[index],"drawable",getPackageName()));
where index is position of item in string name array.

Categories

Resources