Android - cannot populate a TableView with JSONArray data - android

i'm tryng to populate a TableLayout witth data that i get from a MySql database, i get the data from a php script that execute the query and return a JSONArray.
The query seem to work and i get a correct JSONArray, but i get a NullPointerExeption while running this code:
JSONArray jArray = new JSONArray(result);
TableLayout tl = (TableLayout) findViewById(R.layout.list);
TableRow tr = new TableRow(this);
TextView title = new TextView(this);
TextView author = new TextView(this);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
title.setText(json_data.getString("title").toString());
author.setText(json_data.getString("author").toString());
tr.addView(title);
tr.addView(author);
tl.addView(tr);
tr.removeAllViews();
}
"result" contain a valid JSONArray, probably something go wrong in the for cycle, but i don't understand where is the error!
Thanks for your help!

Related

JSON Array Displaying only last element Android TextView

I am trying to list the JSON array and display in the TextViews, but somehow it shows only last element of the JSON array, please suggest the solution, here is my code.
JSONObject jObj = new JSONObject(response);
JSONObject jsonData = jObj.getJSONObject("data");
JSONArray jsonarr = jsonData.getJSONArray("order_status_list");
for (int i1 = 0; i1 < jsonarr.length(); i1++) {
JSONObject c1 = jsonarr.getJSONObject(i1);
txt_order_datetime.setText(c1.optString("status_datetime"));
txt_order_status.setText(c1.optString("order_status_value"));
txt_order_updatedby.setText(c1.optString("status_updated_by_user"));
}
Thanks
As suggested by #sector11, you can use
txt_order_datetime.append(c1.optString("status_datetime"));

Updating Multiple TextView in layout with data from json respose

I want to populate multiple Text Views in layout with JSON data.
I'm getting JSON data but Text View's text is not updated.
Need help!
Here is my code.
JSONObject response = new JSONObject(result1);
JSONArray json_profile_image = response.optJSONArray("profile_image");
for (int i = 0; i < json_profile_image.length(); i++) {
JSONObject jsonObject1 = json_profile_image.getJSONObject(i);
String first_name = jsonObject1.getString("firstname").toString();
String middle_name = jsonObject1.getString("middlename").toString();
String last_name = jsonObject1.getString("lastname").toString();
lbl_firstname.setText(first_name);
lbl_middlename.setText(middle_name);
lbl_lastname.setText(last_name);
}
Got a solution!
I first created an arraylist-
ArrayList<String> dataList = new ArrayList<String>();
then added values from json to it-
dataList.add(first_name);
dataList.add(middle_name);
dataList.add(last_name);
And then updated Text Views -
if(!dataList.isEmpty()){
lbl_firstname.setText(dataList.get(0));
lbl_middlename.setText(dataList.get(1));
lbl_lastname.setText(dataList.get(2));
}
Since I used AsyncTask to get data in json from url, I updated Text Views in
onPostExecute()
Create textview dynamically inside the for loop as below
for (int i = 0; i < json_profile_image.length(); i++) {
JSONObject jsonObject1 = json_profile_image.getJSONObject(i);
String first_name = jsonObject1.getString("firstname").toString();
String middle_name = jsonObject1.getString("middlename").toString();
String last_name = jsonObject1.getString("lastname").toString();
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(first_name);
yourLinearLayout.addView(tv);
}
create textview for all json objects

How to use dynamic textview from json in android

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

Android JsonArray Retrurn and View with Table Layout

I would like to view JsonArray Response and view with Table Layout in android.
Json Response is like this!
[{"id":1,"name":"hot drink"},{"id":2,"name":"cold drink"},{"id":3,"name":"myanmar food"},{"id":4,"name":"thai food"}]
For My Result XML file , I wanna See Like this with TableLayout
ID Name
1 Hot Drink
2 Cold Drink
3 Food
String[] id,name;
JSONArray jsonarrayobj = new JSONArray(yourjsonvalue);
id = new String[jsonarrayobj.length()];
name = new String[jsonarrayobj.length()];
for(int i=0;i<jsonarrayobj.length();i++){
JSONObject jobj = jsonarrayobj.getJSONObject(i);
id[i] = jobj.getString("id");
name[i] = jobj.getString("name");
}
Now you'll have the values in the id and name arrays.
Now with the help of this you can get the view as you said.

Android: JSON data loaded into adapter

This might be a simple Java issue I'm struggling with but I'm looking to the community to assist me here as I have hit a brick wall with this situation.
I successfully have data coming in off of a MySQL database and brought to the application through JSON. During parsing I was trying to create another array to be passed onto an ArrayAdapter to be used in a ListView. Here is the code I'm having an issue with:
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getString("ID");
ct_name=json_data.getString("Player2N");
Games game_data[] = new Games[]
{
new Games(ct_id, ct_name)
};
}
GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, game_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
This line: GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, game_data);
More specifically the game_data is highlighted red in Eclipse. Where I am curious is why does game_data get out of reach after end of loop? I'm just trying to add specific fields within the row from JSON to the adapter here.
I also tried going through the loop within setting up the array but still no dice as new Games[] gets an error. Here is an example:
Games game_data[] = new Games[]
{
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getString("ID");
ct_name=json_data.getString("Player2N");
// Games game_data[] = new Games[]
// {
new Games(ct_id, ct_name);
// };
}
error: Variable must either provide dimension expressions or array initializer.
Hope this helps...
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
Games game_data[];
game_data[] = new Games[jArray.length()];
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getString("ID");
ct_name=json_data.getString("Player2N");
game_data[i] = new Games(ct_id, ct_name);
}
GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, game_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
I am thinking that Games is your POJO class so you can better do something like this,
Create a List of the POJO class
private List<Games> games= new ArrayList<Games>();
Now you can add your content in the games list.
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getString("ID");
ct_name=json_data.getString("Player2N");
games.add(new Games(ct_id, ct_name));
}
And finally pass the list in the Adapter.
GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, games);
error: Variable must either provide dimension expressions or array initializer.
=> This error is obvious because you haven't define any dimensions value to create an array and also haven't passed any initialization to this array.
So I think you are doing wrong, instead do it like:
Games game_data[] = new Games(ct_id, ct_name);
This is the correct way to call a constructor.
And i don't know what you have written inside the Games class so i just have mentioned about the calling of constructor.

Categories

Resources