How to parse data under Textview - android

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));

Related

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

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 do update one textview text when the other textview text got updated

Hi am developing android application. I have some small doubt of how to change one textview text when the other textview text got changed.
Let us say i have fourtext fields n1,n2 for displaying names and s1,s2 for displaying scores.n1=android1,n2=android2,s1=30,s2=50
After applying sort for scores(descending) am able to sort scores but the names still in same text view.how can i change when names too in this example.
After sort n1=android1,n2=android2,s1=50,s2=30
Thanks in advance...please respond soon if any knows how can i do this...
Suppose in your name array
String[] name={"yuvi","sachin","dravid","sehvag"};
int[] score={15,8,89,10};
for(int i=0;i<score.length;i++){
for(int j=i+1;j<score.length;j++){
if(score[i] < score[j]){
int tempsc=score[i];
String tempname=name[i];
score[i]=score[j];
name[i]=name[j];
score[j]=tempsc;
name[j]=tempname;
}
}
}
You can also try like this...
EditText[20] e1;
String[10] names;<----- array of your names
Int[10] scores;<----- array of your scores
String[10] temp;
for(int g=0;g<10;g++)
{
temp[g]=String.valueOf(scores[g])+"-"+names[g];
}
Arrays.sort(temp);
for(int g=0;g<10;g++)
{
String[] T=temp[g].split("-")
e1[g].setText(t[1]);<----- name
e1[g+10].setText(t[0]);<----- score
}
think this is much easy.. =]

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]);

setting text in an array of buttons

I'm trying to create a simple application in which there are 30 buttons and I need to initialize their text field.
I created this array of buttons:
Button[][] buttons_arr = new Button[10][3];
To change each button's text I did :
for(i=0..9) //psaudo
for (j=0..29) //psaudo
buttons_arr[i][j].setText(toString(some_int));
The last line is causing some problems. Why and what can I do to solve this issue ?
You are actually looping in 300 times instead of 30 times
try like this
for(i=0..9) //psaudo
for (j=0..2) //psaudo
buttons_arr[i][j].setText(""+some_int);
Try this:
Button[][] b=new Button[10][3];
for(int i=0;i<10;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=new Button(context);
b[i][j].setText("something");
}
}
I have not tried 2D arrays. But my experience with a similar problem seems to be that buttons_arr[i][j] is still uninitialized. You need to either create a new button:
buttons_arr[1][1] = new Button();
or
buttons_arr[1][1] = (Button)findViewById(R.id.buttonAtPosition1_1);
try this
for(int i=0;i<10;i++){
for(int j=0;j<3;j++)
buttons_arr[i][j].setText(your text);
}

Categories

Resources