how to convert int into string in forloop - android

I want to print json array utrition arrray in textview dynamically problem is that if I write"1" in this line (school3.getJSONObject("1"));
then its print only first "name" and "Qty" from json. I want to write j at this line but problem is it shows error The method getJSONObject(String) in the type JSONObject is not applicable for the arguments (int)
for (int j = 1
school3.getJSONObject(j)
is there any method convert int j= string then add in
school3.getJSONObject(j) like this way
//for example
string z;
z=j.tostring();
school3.getJSONObject(z);
{
"status":1,
"data"
,
"dish_nutrition":
{
"1":
{
"name":"Cholesterol and Diet",
"qty":"2"
},
"2":
{
"name":"Cholesterol and Diet",
"qty":"1"
}
}
}
JSONObject school3 = json2.getJSONObject("dish_nutrition");
final TableLayout table = (TableLayout) findViewById(R.id.table2);
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject("1"));
table.addView(row);
num=num+1;
}
public View createRow(JSONObject item) throws JSONException {
View row = getLayoutInflater().inflate(R.layout.rows, null);
((TextView)
row.findViewById(R.id.localTime)).setText(item.getString("name"));
((TextView)
row.findViewById(R.id.apprentTemp)).setText(item.getString("qty"));
return row;
}

Use Integer.toString(int) or use int+"" in converts the integer to string like j+""
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject(j+""));
table.addView(row);
num=num+1;
}
OR
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject(Integer.toString(j)));
table.addView(row);
num=num+1;
}

String z = String.valueOf(j);
school3.getJSONObject('\"' + z + '\"');

Integer to string?
Integer.toString(i)

You can do something like this :
school3.getJSONObject('\"' + j + '\"');

If you need to convert int into string just use String.valueof(int values);
and if u need to convert string to int use
Integer.parseInt(string values);

Through it is basic java. you should know that.
String.valueof(int values) to convert Int to String
Integer.parseInt(string values) to convert String to Int

Related

Getting an attribute from an object inside an arraymap

I'm creating a class called "partite", and this is the code
public class Partita {
String HT; //HomeTeam
String AT; //AwayTeam
String dataP; //dataPartita
int HG; //HomeGoal
int AG; //AwayGoal
String FTR; //Full time result
public Partita(){
this.HT = "";
this.AT = "";
this.dataP = "";
this.HG = 0;
this.AG = 0;
this.FTR = "";
}
public Partita(String HT, String AT, String dataP, int HG, int AG, String FTR) {
this.HT = HT;
this.AT = AT;
this.dataP = dataP;
this.HG = HG;
this.AG = AG;
this.FTR = FTR;
}
}
In the main activity I'm creating an ArrayList, putting a list of some "Partita" object, with attributes come from a json file, and then I create an ArrayMap and put the ArrayList inside, like this
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
partite.add(new Partita(jsonPart.getString("HomeTeam"),
jsonPart.getString("AwayTeam"),jsonPart.getString("Date"),
jsonPart.getInt("FTHG"), jsonPart.getInt("FTAG"),
jsonPart.getString("FTR")));
partitemap.put(i, partite.get(i));
Log.i("partita", partite.get(i).HT + " " + partite.get(i).HG + ":" + partite.get(i).AG + " " + partite.get(i).AT);
}
How can I use the ArrayMap instead of ArrayList to get the attributes of an object and use it?
Use this instead (edited):
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
partitemap.put(i, new Partita(jsonPart.getString("HomeTeam"),
jsonPart.getString("AwayTeam"),
jsonPart.getString("Date"),
jsonPart.getInt("FTHG"),
jsonPart.getInt("FTAG"),
jsonPart.getString("FTR")
)
);
Log.d("tag", partitemap.get(i).HT);
}

display Multidimensional Arrays android

I'm using android studio and with opencv 3.4.0. I want to display a Multidimensional Array in text view. I have an a array "candidats_result" and I copied her values into a Multidimensional array " finale". after runing my code I got this result in emulator display. what I have to change in my code to get the text displayed
double [][] R_finale = new double[20][20];
int ZZ = 0;
int ZE = 0;
int EE = 0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
R_finale[i][j] = candidats_result[ZZ];
ZZ++;
}
}
String [][] finale = new String [20][20];
//showing the array in android
for(int i=0;i<19;i++) {
for (int j = 0; j < 19; j++) {
finale[i][j] = Double.toString(R_finale[i][j]);
}
}
String [][] details = new String[20][20];
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";");
}
}
textView.setText(String.valueOf(builder));
Notice below lines:
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";"); //should be builder.append(details[i][j] + ";");
}
}

for loop overextending index

I'm attempting to iterate through the EditText "name" and check the letters so see if they match any of the letters in the objects of the array uNamesList.
If they do I want to break out of the loop and return the placement of the object that had the matching letter. At the moment I am getting this error, anyone know what might be wrong?
java.lang.StringIndexOutOfBoundsException: length=9; index=9
uNamesList.add("bob");
uNamesList.add("mike");
uNamesList.add("sike");
uNamesList.add("othername");
uNamesList.add("name");
public int getName(EditText name) {
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
break;
}
}
}
return match;
}
You get the text inside edit text in wrong way. it should :
public int getName(EditText name) {
String text = name.getText(); //get the text
int match = 10;
for (int i = 0; i < text.toString().length(); i++) { //get the length of the text
for (int o = 0; o < uNamesList.size(); o++) {
char cLetter = name.toString().charAt(i);
String uName = (String)uNamesList.get(o);
Okay, here's the edit for another problem. i dont know if this what you want.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
boolean found = false;
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
found = true;
break;
}
}
if(found) break;
}
return match;
}
The simplest way is just only call return when it found.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
return o;
}
}
}
return match;
}
change your for loop to the following, you accessing name from the uNameList loop where the length might be different
for (int i = 0; i < name.toString().length(); i++) {
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
For EditText you need GetText()
name.toString() gives you the java desciption of the object EditText
You are getting a StringIndexOutOfBoundsException most likely due to this line:
char uLetter = uName.charAt(i);
i in this case can be greater than the length of uName

Create a dynamic table of records, android?

I need to add at least 10 tables in my application to show some statistics to the user.
The data of the table comes from the server side even the table headers.
Instead of creating 10 different tables I am looking for an approach where I can reuse the table. Changing the size at runtime.
In what data structure should I store the table values and then display in the table?
All I end up doing was this
For header
static void header() {
TableRow tb_head = new TableRow(c);
for (int i = 0; i < row.size(); i++) {
TextView tv1 = new TextView(c);
tv1.setText(row.get(i));
tb_head.addView(tv1);
}
table.addView(tb_head);
}
For table values
static void tableValues()
{
for (int i = 0; i < row.size()-1; i++) {
TableRow tbrow = new TableRow(c);
for (int j = 0; j <= 7; j++) {
TextView tv1 = new TextView(c);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
tv1.setId(id);
tv1.setText("TextView no: " + id);
tbrow.addView(tv1);
}
table.addView(tbrow);
}
}
Any help would be much appreciated.
hope these tutorial links may help you
http://www.lucazanini.eu/2012/android/dynamic-tablelayout-in-android/?lang=en
http://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/
http://www.tutorialsbuzz.com/2014/02/android-building-tablelayout-at-runtime.html
http://wowjava.wordpress.com/2011/04/01/dynamic-tablelayout-in-android/
http://www.tutorialsbuzz.com/2014/01/android-loading-sqlite-data-tablelayout.html

Android from string to string array

hi to all i have this code have this code which reads a some text and it extracts any strings between the '[' and ']' and it should print it on the screen
String lines[] = {addressString};
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
String fields = lines[i].substring(be+1, e);
}
my question is that i want to change the string "fields" to an array string so when i print it
i can print it as fields[0],fields[1],....etc until the end of the text....?
any suggestions...??
Thanks a lot
Is this what you mean?
String lines[] = {addressString};
String fields[] = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
fields[i] = lines[i].substring(be+1, e);
}

Categories

Resources