I am using post method to get this array and number of columns will increase based on every post.
"floor":
[
{"no_of_bedroom":"1.5","floor_plotsize_start":"692.00","floor_price_start":"4356832.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1000.00","floor_price_start":"6296000.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1029.00","floor_price_start":"6478584.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1132.00","floor_price_start":"7127072.00"},
{"no_of_bedroom":"3.0","floor_plotsize_start":"1390.00","floor_price_start":"8751440.00"},
{"no_of_bedroom":"3.0","floor_plotsize_start":"4901.00","floor_price_start":"40801320.00"}
]
How to display by using dynamic textview in android?
JSONArray jsonarray;
jsonarray = jsonobject.getJSONArray("floor");
{
//How to proceed by using dynamic textview?
}
Thanks in advance
I guess you can create a listview with textview of the item that you would like to display
My suggestion would be using a loop to get per object and place it inside the listview
peseudo code:
for(int i=0; i< jsonarray.size(); i++){
list.add(jsonarray.getJSONObject(i).getString("no_of_bedroom")); //just an example to check the details of no_of_bedroom key and add it inside the
}
//using list adapter HERE to display the item the TextView
It would be good for you to try it out and update us with more information of how you would like to do it.
You can dynamically create the textviews you want,then you can add them to your main layout and display. Below is a small hint for it:
JSONArray jArray = jsonobject.getJSONArray("floor");
for(int i=0; i < jArray.length(); i++)
{
TextView textview = new TextView();
textview.setText(""); //your own text
textview.setTextColor(Color.parseColor("#000000")); //your own color
//And now add this textview to your main layout
yourlayout.addView(textview);
}
try this
JSONArray jArray = jsonobject.getJSONArray("floor");
for(int i=0; i < jArray.length(); i++)
{
TextView textview = new TextView(this);
textview.setText(""); //your own text
layoutname.addView(textview);
}
Related
How can I fill views using cycles. For example, I have a three element:
TextView tv_1, tv_2, tv_3
Can I do something like this?
for(int i=1; i<=3; i++){
tv_{i}.setText(i);
}
Just create an List of textViews and run cycles/loop on it.
List<TextView> textViews = new ArrayList<>();
textViews.add(textView1);
textViews.add(textView2);
textViews.add(textView3);
after that just iterate through it :
for(int i=0 ;i <textViews.size(); i++)
{
textViews.get(i).setText("text");
}
Here, try this.
TextView[] tvs = new TextView[3];
tvs[0] = findViewById(R.id.tv1);
tvs[1] = findViewById(R.id.tv2);
tvs[2] = findViewById(R.id.tv3);
for(int i=0; i<3; i++){
tvs[i].setText(i);
}
{
layout:[
{
tag :"edittext",
name :"Name",
hint :"Type your name here"
},
{
tag :"checkbox",
name :"Is married",
hint :""
},
{
tag :"button",
name :"Submit",
hint :""
}
]
}
I am describing what exactly i want . First of all the above json will change every time. Structure will same , just tag, name and hint value will change . Now the above jsonarray has three jsonobject . it might be any number(4/5/6 any number of jsonobject) . Can any one plz suggest me how to solve this issue ? Thanks
Its better to divide your question in small small parts, so here is something i have for your
Parse JSON and fetch its value : I suggest you to follow How to parse JSON in Android this link and you will have everything from your json.
Check whether control is EditText or Button or anything else : I will suggest you to use switch case for better coding structure
switch (tag ){
case "edittext":
//add edittext
break;
case "button":
//add button
break;
}
follow this links to add controls dynamically
Generating Edit Text Programatically in android
Add button to a layout programmatically
How to add checkboxes dynamically in android
You can create your layout according to the size of jsonarray.
JSONArray jArray = jObject.getJSONArray("layout");
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject oneObject = jArray.getJSONObject(i);
// Pulling items from the array
String tag = oneObject.getString("tag");
String name= oneObject.getString("name");
String hint= oneObject.getString("hint");
Button button = new Button(this);
button.setText(name);
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(buyButton);
} catch (JSONException e) {
// Oops
}
}
Hello I set values in ArrayList Now how can I store that values in different textviews.
For Example:
ArrayList<String> mylist =new ArrayList<String>();
for(int i=0;i<mylist.size();i++)
{
//Here mylist contains 10 values and I have 10 different textviews. Now How can i add values 1 to 10 in different textview. set value to First textview 1,second textview to 2 etc.
}
Please Help me to find this.
This is the way::
TextView []tv=new TextView[10];
tv[0]=(TextView)findViewById(R.id.____);
tv[1]=(TextView)findViewById(R.id.____);
tv[2]=(TextView)findViewById(R.id.____);
tv[3]=(TextView)findViewById(R.id.____);
tv[4]=(TextView)findViewById(R.id.____);
tv[5]=(TextView)findViewById(R.id.____);
tv[6]=(TextView)findViewById(R.id.____);
tv[7]=(TextView)findViewById(R.id.____);
tv[8]=(TextView)findViewById(R.id.____);
tv[9]=(TextView)findViewById(R.id.____);
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("1");
mylist.add("2");
mylist.add("3");
mylist.add("4");
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < mylist.size(); i++)
{
TextView tv = new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(mylist.get(i));
layout.addView(tv);
}
And XML File
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
If u have the same no of text views and the data then create a array of views then use for loop as:
ArrayList<String> mylist =new ArrayList<String>();
mylist.add("1st");
mylist.add("2nd");
mylist.add("3rd");
mylist.add("4th");
// mylist.add("5th");
one=(TextView)findViewById(R.id.textView1);
two=(TextView)findViewById(R.id.textView2);
three=(TextView)findViewById(R.id.textView3);
four=(TextView)findViewById(R.id.textView4);
ArrayList<TextView>text=new ArrayList<TextView>(Arrays.asList(one,two,three,four));
for(int i=0;i<mylist.size();i++)
{
text.get(i).setText(mylist.get(i));
//Here mylist contains 10 values and I have 10 different textviews. Now How can i add values 1 to 10 in different textview. set value to First textview 1,second textview to 2 etc.
}
Then you dont need of for loop. just set text to Your text views manually
as
ArrayList<String> mylist =new ArrayList<String>();
TextView txt1 = (TextView)findviewbyId(R.id.txt1);
txt1.setText(mylist.get(0);
....//upto 10 textViews
TextView txt10 = (TextView)findviewbyId(R.id.txt10);
txt10.setText(mylist.get(9);
You could try something like this:
for(int i=0;i<mylist.size();i++)
{
TextView tv = findViewById(getResources().getIdentifier("textView"+i, "id",getPackageName()));
tv.setText(mylist.get(i));
}
I'm not sure that is you want to do. I used the getIdentifier method to retrieve your textViews.
Else, you can inflate your textViews in a loop:
for(int i=0;i<mylist.size();i++)
{
TextView tv = (TextView)LayoutInflater.from(context).inflate(R.layout.myTextView, null);
tv.setText(mylist.get(i));
}
Hope this will help you
You dont need loop here
you need to call textView.setText(mylist.get(0)); ... u need to call this for 10 times with increasing value in get method.. and with diff text view instances.
Edit :as par your comment: here suppose you have created
TextView [] tv; // you need to initialized array here with textviews
ArrayList<String> mylist =new ArrayList<String>();
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}
I need to create an game info list using this json response in android:
{"count":"2","useruid":"100003264774181","games":[{"lastupdate":"2012-01-17 13:55:39","gametype":"R","players":{"2":"100002913406085,Prabir Kumar D,6sq6","1":"100003264774181,Avinash S,twyq"},"turnuid":"100002913406085","score":{"2":"0","1":"18"},"playerscount":2,"dic":"twl","active":"y","canforcewin":"n","showmsg":"n","lastmove":"HIDE,r,18","gameid":"85030616","tilesinbag":"68"},{"lastupdate":"2012-01-17 13:50:55","gametype":"R","players":{"2":"100000145226351,Sayak K,0r9o","1":"100003264774181,Avinash S,kltv"},"turnuid":"100000145226351","score":{"2":"0","1":"26"},"playerscount":2,"dic":"twl","active":"y","canforcewin":"n","showmsg":"n","lastmove":"BOX,r,26","gameid":"85030764","tilesinbag":"68"}]}
my xml is linearlayout>>frame layout>> scrollview>> linear layout-- android:orientation="horizontal">> text view
currently i m using this but got stuck..
JSONObject jresponse=response.getJsonResponseFor(myRequest);
String result=jresponse.toString();
System.out.println(" jresponse is here "+jresponse.toString());
JSONArray jArray = jresponse.getJSONArray("games");
int max=jArray.length();
System.out.println("array length::"+max);
for (int j = 0; j < max; j++)
{
JSONObject obj = jArray.getJSONObject(j);
JSONArray names = obj.names();
for (int k = 0; k < names.length(); k++)
{
String name = names.getString(k);
String value= obj.getString(name);
createtableLayout();
}
}
the createtableLayout() should add the data dynamically to the xml.
Please help me regarding this.
You need not add data dynamically to the xml. You can just create some base layout in XML.
You can get that layout using findviewbyid and then keep adding the data using the java for that layout.
You can find many samples of creating /adding layouts in java.
ok, then add a layout in your xml, and have a reference of this layout as a field in activity,
Create views like:
for Layouts:
LinearLayout ll=new LinearLayout(this);
to set layout param on view or layout use:
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
example to create views,
TextView txt= new TextView(this);
txt.setText(name);
To add view to layout use:
ll.addView(txt);
to add layout to parent layout, use:
parent.addView(ll);
use method like following,
holder=(LinearLayout)findViewById(R.id.GameListHolder);
JSONObject jresponse=response.getJsonResponseFor(myRequest);
String result=jresponse.toString();
System.out.println(" jresponse is here "+jresponse.toString());
JSONArray jArray = jresponse.getJSONArray("games");
int max=jArray.length();
System.out.println("array length::"+max);
for (int j = 0; j < max; j++)
{
JSONObject obj = jArray.getJSONObject(j);
JSONArray names = obj.names();
for (int k = 0; k < names.length(); k++)
{
String name = names.getString(k);
String value= obj.getString(name);
//Fetch values here
createtableLayout(opponentName, lastMove,points, time, sinceLastMove, whose turn);
}
}
public void createtableLayout(String opponentName, String lastMove, String points,
String time, String sinceLastMove, String whoseTurn)
{
LinearLayout ll=new LinearLayout(this);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txtOppName=new TextView(this);
txtOppName.setText(txtOppName);
ll.addView(txtOppName);
TextView txtOppName=new TextView(this);
txtOppName.setText(txtOppName);
ll.addView(txtOppName);
......
holder.addView(ll);
}
}
In my application, i have a tableLayout with many editTexts in it. When i click "save"button, i want to access all the values entered in editTexts. I have assigned IDs in runtime while creating the table. Now how can i access the values from editTexts when "save" button is clicked...? I have used something like below to assign IDs,
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
...............
assignment.setId(i+j);
.............
}
Could anyone suggest a solution..?
Other nice solution is to cycle through all childrens of some Layout, so you don't need to set any special IDs. Just try:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();
for (int i=0; i<child_count; i++)
{
EditText text = (EditText) layout.getChildAt(i);
// do work with text
}
With some other code, you can do this for any other layout hierarchy.
How about something like:
ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
EditText text = (EditText)ActivityName.this.findViewById(i+j);
strings.add(text.getText().toString());
}
}
This would give you all of the values from all of the texts in one big array. If that's not what you want, let me know and I'll see if I can adjust the code.