hi in the below I am fetching the data from database and getting the json response and displaying in an android table layout.But everything was working fine but i want to put some sub headings for displaying data for that I took the textview In that using setText() I am giving sub heading.But it's not displaying proper heading.
this output when I am running my app:
General registration
SI Date Toatal Registration
1 2015-04-01 2
2 2015-04-02 3
1 2015-04-02 3
Expected output:
SI Date Toatal Registration
General registration
1 2015-04-01 2
2 2015-04-02 3
Ipd registration
1 2015-04-02 3
java
public class Datewise_Patient_list extends Activity {
String data = "",result="";
TableLayout tl;
TableRow tr;
TextView si,tv;
String queryString;
int numOfPatients = 0;
TextView[] snos;
TextView[] date;
TextView[] tot_reg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datewise_collection_report);
tl = (TableLayout) findViewById(R.id.maintable);
tv=(TextView)findViewById(R.id.tv);
final String date1 = getIntent().getStringExtra("date1");
final String date2 = getIntent().getStringExtra("date2");
new Thread(new Runnable() {
public void run() {
queryString = "date1=" + date1 + "&date2="
+ date2;
data = DatabaseUtility.executeQueryPhp("DateWise_Collection_report",queryString);
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<DateWise_Patients> datewisepatients = parseJSON(data);
numOfPatients = datewisepatients.size();
addData(datewisepatients);
}
});
}
}).start();
new Thread(new Runnable() {
public void run() {
queryString = "date1=" + date1 + "&date2="
+ date2;
result = DatabaseUtility.executeQueryPhp("DateWise_ipd_report",queryString);
System.out.println(result);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<DateWise_Patients> datewiseipdpatients = parseJSON1(result);
numOfPatients = datewiseipdpatients.size();
addData1(datewiseipdpatients);
}
});
}
}).start();
}
public ArrayList<DateWise_Patients> parseJSON(String data) {
ArrayList<DateWise_Patients> datewisepatients = new ArrayList<DateWise_Patients>();
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
DateWise_Patients datewisepatient = new DateWise_Patients();
datewisepatient.setDate(json_data.getString("date"));
datewisepatient.setTot_reg(json_data.getString("total"));
datewisepatients.add(datewisepatient);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return datewisepatients;
}
public ArrayList<DateWise_Patients> parseJSON1(String result) {
ArrayList<DateWise_Patients> datewiseipdpatients = new ArrayList<DateWise_Patients>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
DateWise_Patients datewiseipdpatient = new DateWise_Patients();
datewiseipdpatient.setDate(json_data.getString("date"));
datewiseipdpatient.setTot_reg(json_data.getString("total"));
datewiseipdpatients.add(datewiseipdpatient);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return datewiseipdpatients;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
si = new TextView(this);
si.setText("SI");
si.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
si.setPadding(5, 5, 5, 5);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.addView(si,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView date = new TextView(this);
date.setText("Date");
date.setTextSize(15);
date.setTextColor(Color.parseColor("#FFA500"));
date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
date.setPadding(5, 5, 5, 5);
// pat_name.setBackgroundColor(Color.parseColor("#075E93"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(date,params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
TextView tot_reg = new TextView(this);
tot_reg.setText("Total Registrations");
tot_reg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tot_reg.setPadding(5, 5, 5, 5);
//reg_date.setBackgroundColor(Color.parseColor("#075E93"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(tot_reg,params);
tr.addView((View)Ll); // Adding textview to tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
#SuppressWarnings({ "rawtypes", "deprecation" })
public void addData(ArrayList<DateWise_Patients> datewisepatients) {
snos = new TextView[numOfPatients];
date = new TextView[numOfPatients];
tot_reg=new TextView[numOfPatients];
int j = 0;
addHeader();
tv.setText("General Registration");
for (Iterator i = datewisepatients.iterator(); i.hasNext();) {
snos[j] = (TextView) findViewById(R.id.si);
date[j] = (TextView) findViewById(R.id.date);
tot_reg[j]=(TextView)findViewById(R.id.tot_reg);
DateWise_Patients p = (DateWise_Patients) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
if(j % 2==0){
tr.setBackgroundColor(Color.parseColor("#00BFFF"));
}
else{
tr.setBackgroundColor(Color.parseColor("#FFFFE0"));
}
/** Creating a TextView to add to the row **/
snos[j] = new TextView(this);
int temp = j;
snos[j].setText(String.valueOf(temp +1));
snos[j].setId(j);
snos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
snos[j].setPadding(5, 5, 5, 5);
// snos[j].setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(snos[j],params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
date[j] = new TextView(this);
date[j].setKeyListener(null);
date[j].setText(p.getDate());
date[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
date[j].setPadding(5, 5, 5, 5);
// patNames[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(date[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
tot_reg[j] = new TextView(this);
tot_reg[j].setKeyListener(null);
tot_reg[j].setText(p.getTot_reg());
tot_reg[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tot_reg[j].setPadding(5, 5, 5, 5);
// regdates[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(tot_reg[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
j++;
}
}
#SuppressWarnings({ "rawtypes", "deprecation" })
public void addData1(ArrayList<DateWise_Patients> datewiseipdpatients) {
snos = new TextView[numOfPatients];
date = new TextView[numOfPatients];
tot_reg=new TextView[numOfPatients];
int j = 0;
// tv.setText("Ipd Patient List");
for (Iterator i = datewiseipdpatients.iterator(); i.hasNext();) {
snos[j] = (TextView) findViewById(R.id.si);
date[j] = (TextView) findViewById(R.id.date);
tot_reg[j]=(TextView)findViewById(R.id.tot_reg);
DateWise_Patients p = (DateWise_Patients) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
if(j % 2==0){
tr.setBackgroundColor(Color.parseColor("#00BFFF"));
}
else{
tr.setBackgroundColor(Color.parseColor("#FFFFE0"));
}
/** Creating a TextView to add to the row **/
snos[j] = new TextView(this);
int temp = j;
snos[j].setText(String.valueOf(temp +1));
snos[j].setId(j);
snos[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
snos[j].setPadding(5, 5, 5, 5);
// snos[j].setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(snos[j],params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
date[j] = new TextView(this);
date[j].setKeyListener(null);
date[j].setText(p.getDate());
date[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
date[j].setPadding(5, 5, 5, 5);
// patNames[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(date[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
/** Creating Qty Button **/
tot_reg[j] = new TextView(this);
tot_reg[j].setKeyListener(null);
tot_reg[j].setText(p.getTot_reg());
tot_reg[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tot_reg[j].setPadding(5, 5, 5, 5);
// regdates[j].setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(tot_reg[j],params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
j++;
}
}
Related
Currently I am working with a project to display database value in a table in Android vertically.I got the answer in horizontally, now I want it in vertically.
.
.
Main Activity.java
public class MainActivity extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
final GetDataFromDB getdb = new GetDataFromDB();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONObject jsono = new JSONObject(result);
//JSONArray jArray = new JSONArray("users");
JSONArray jArray = jsono.getJSONArray("agriculture1");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setCrop(json_data.getString("crop"));
user.setCategory(json_data.getString("category"));
user.setSoil_texture(json_data.getString("soil_texture"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Crop");
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(30, 30, 30, 30);
label.setBackgroundColor(Color.LTGRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText("Category");
place.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
place.setPadding(30, 30, 30, 30);
place.setBackgroundColor(Color.LTGRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView soil = new TextView(this);
soil.setText("Soil_Texture");
soil.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
soil.setPadding(30, 30, 30, 30);
soil.setBackgroundColor(Color.LTGRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(soil,params);
tr.addView((View)Ll); // Adding textview to tablerow
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
#SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getCrop());
label.setId(p.getId());
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(10,10, 10, 10);
label.setBackgroundColor(Color.CYAN);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getCategory());
place.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
place.setPadding(10, 10, 10, 10);
place.setBackgroundColor(Color.CYAN);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView soil = new TextView(this);
soil.setText(p.getCategory());
soil.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
soil.setPadding(10, 10, 10, 10);
soil.setBackgroundColor(Color.CYAN);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(soil,params);
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_below="#+id/textView1">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:stretchColumns="*"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
Do you have to use 1 table? You can split your layout and use two tables one for "header" and one for values.
You can split your layout like this:
<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp"/>
</LinearLayout>
Then add your tables inside the inner layouts.
I am trying to display my json data in table layout and for that I have prepare one phpwebservice which run fine without error and I also got Json data at the android but I don't understand why it is not populating with tablelayout and my app unfortunately has stopped.here is my code..
TableActivity
package com.example.my.phploginregistration;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class TableActivity extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
private static final String Json_URL ="http://10.0.2.2:8080/getRecordForTableView.php";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
tl = (TableLayout) findViewById(R.id.main_table);
final GetDataFromDB getdb = new GetDataFromDB();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB(Json_URL);
System.out.println("data::" + data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setName(json_data.getString("name"));
user.setUsername(json_data.getString("username"));
user.setEmail(json_data.getString("email"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Name");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView(Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView uname = new TextView(this);
uname.setText("Username");
uname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
uname.setPadding(5, 5, 5, 5);
uname.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(uname,params);
tr.addView(Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating Qty Button **/
TextView email = new TextView(this);
email.setText("email");
email.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
email.setPadding(5, 5, 5, 5);
email.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(email,params);
tr.addView(Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
#SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getName());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView(Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView un = new TextView(this);
un.setText(p.getUsername());
un.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
un.setPadding(5, 5, 5, 5);
un.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(un,params);
tr.addView(Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating Qty Button **/
TextView mail = new TextView(this);
mail.setText(p.getEmail());
mail.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mail.setPadding(5, 5, 5, 5);
mail.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(mail,params);
tr.addView(Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
Here is my full json response...
{
"Users":[
{
"id":"1",
"name":"vandana rao",
"username":"vr",
"email":"vr#gmail.com"
},
{
"id":"2",
"name":"hemant rao",
"username":"hr",
"email":"hr#gmail.com"
},
{
"id":"3",
"name":"kirti rao",
"username":"kr",
"email":"kr#gmail.com"
},
{
"id":"4",
"name":"bhavana rao",
"username":"br",
"email":"br#gmail.com"
},
{
"id":"5",
"name":"nilesh rao",
"username":"nr",
"email":"nr#gmail.com"
},
{
"id":"6",
"name":"rishika rao",
"username":"rr",
"email":"rr#gmail.com"
},
{
"id":"7",
"name":"jitu",
"username":"jr",
"email":"jr#gmail.com"
},
{
"id":"8",
"name":"kushi",
"username":"krr",
"email":"krr#gmail.com"
},
{
"id":"9",
"name":"yogi",
"username":"yr",
"email":"yr#gmail.com"
},
{
"id":"10",
"name":"kavish",
"username":"kvi",
"email":"kvi#gmail.com"
},
{
"id":"11",
"name":"kaviya",
"username":"kaviya",
"email":"kaviya#gmail.com"
},
{
"id":"12",
"name":"bharti",
"username":"bh",
"email":"bh#gmail.com"
}
],
"success":1
}
error in following code.......
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
you are parsing JSONArray from your String result, but it is JSONOject so you have to passing like this...
JSONObject jObj = new JSONObject(result);
JSONArray jArray =jObj.getJSONArray("Users");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setName(json_data.getString("name"));
user.setUsername(json_data.getString("username"));
user.setEmail(json_data.getString("email"));
users.add(user);
}
The way you have added a row into the table layout you can add multiple TableRow instances into your tableLayout object
Try adding multiple rows as in your code you are adding single row multiple times:
Try below code:
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Name");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView(Ll); // Adding textView to tablerow.
tr1=new TableRow(this);
/** Creating Qty Button **/
TextView uname = new TextView(this);
uname.setText("Username");
uname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
uname.setPadding(5, 5, 5, 5);
uname.setBackgroundColor(Color.RED);
L2 = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
L2.addView(uname,params);
tr1.addView(L2); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr);
tl.addView(tr1);
tr2=new TableRow(this);
/** Creating Qty Button **/
TextView email = new TextView(this);
email.setText("email");
email.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
email.setPadding(5, 5, 5, 5);
email.setBackgroundColor(Color.RED);
L3 = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
L3.addView(email,params);
tr2.addView(L3); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr2);
}
Hope this will work for you.
Hi In this code finding the total marks and setting the value into textView.But it showing last value and output showing 65.Based on marks I want to find the total marks of the student
Can any one please help me.
java
total_marks=new int[numOfStudents];
addHeader();
int j = 0;
for (Iterator i = users.iterator(); i.hasNext();) {
subject[j] = (EditText) findViewById(R.id.subject);
subject[j].setVisibility(View.GONE);
Maxmarks[j] = (EditText) findViewById(R.id.max_mark);
Maxmarks[j].setVisibility(View.GONE);
Minmarks[j] = (EditText) findViewById(R.id.min_mark);
Minmarks[j].setVisibility(View.GONE);
ObtainedMarks[j] = (EditText) findViewById(R.id.obtained_mark);
ObtainedMarks[j].setVisibility(View.GONE);
Grades[j] = (EditText) findViewById(R.id.grade);
Grades[j].setVisibility(View.GONE);
StudentgradeDetails p = (StudentgradeDetails) i.next();
tr = new TableRow(this);
subject[j] = new EditText(this);
subject[j].setKeyListener(null);
subject[j].setText(p.getSubject());
subject[j].setId(p.getId());
subject[j].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
subject[j].setPadding(3, 3, 3, 3);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
Ll.addView(subject[j], params);
tr.addView((View) Ll);
Maxmarks[j] = new EditText(this);
Maxmarks[j].setKeyListener(null);
Maxmarks[j].setText(p.getMaxMark());
Maxmarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Maxmarks[j].setPadding(2,2, 2, 2);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
Ll.addView(Maxmarks[j], params);
tr.addView((View) Ll);
Minmarks[j] = new EditText(this);
Minmarks[j].setKeyListener(null);
Minmarks[j].setText(p.getMinMark());
Minmarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Minmarks[j].setPadding(3, 3, 3, 3);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(Minmarks[j], params);
tr.addView((View) Ll);
ObtainedMarks[j] = new EditText(this);
ObtainedMarks[j].setKeyListener(null);
ObtainedMarks[j].setText(p.getObtainedMark());
ObtainedMarks[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ObtainedMarks[j].setPadding(3, 3, 3, 3);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(ObtainedMarks[j], params);
tr.addView((View) Ll);
Grades[j] = new EditText(this);
Grades[j].setKeyListener(null);
int marks1 =Integer.parseInt(ObtainedMarks[j].getText().toString());
#SuppressWarnings("unused")
String grade=Integer.toString(marks1);
if(marks1 >= 80){
grade="A+";
} else if(marks1>=70){
grade="A";
}
else if(marks1>=60){
grade="B+";
}
else if(marks1>=50){
grade="B";
}
else if(marks1>=40){
grade="C";
}
else{
return;
}
Grades[j].setText(grade);
try {
total_marks[j]+=marks1;
TotalMarks=(TextView)findViewById(R.id.total);
TotalMarks.setText("Total Marks:"+total_marks[j]);
}
catch (Exception e) {
e.printStackTrace();
}
Grades[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Grades[j].setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 3, 3);
Ll.addView(Grades[j], params);
tr.addView((View) Ll);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
j++;
}
use this
int total_marks=0;
int marks1 =Integer.parseInt(ObtainedMarks[j].getText().toString());
total_marks+=marks1;
TotalMarks=(TextView)findViewById(R.id.total);
TotalMarks.setText("Total Marks:"+total_marks);
I am creating online shopping application for android and i have created a table which displays data from the server regarding the availability of the item. I have added a button next to it which adds that item of that row to the cart and it should send data to the server as well regarding which item was selected.
My problem is that i am not able to figure out how to send entire data of the specific row to the server and it should not re-direct from the current screen. Also my server is in php and is ready.
public class Availability extends Activity implements View.OnClickListener {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_availability);
tl = (TableLayout) findViewById(R.id.main_table);
final GetDatafromDB_Availability getdb = new GetDatafromDB_Availability();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Users_availability> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users_availability> parseJSON(String result) {
ArrayList<Users_availability> users = new ArrayList<Users_availability>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users_availability user = new Users_availability();
user.setId(json_data.getInt("id"));
user.setProduct_code(json_data.getString("product_code"));
user.setShapes(json_data.getString("shaps"));
user.setPair(json_data.getString("pair"));
user.setCarats(json_data.getString("carats"));
user.setColor(json_data.getString("color"));
user.setClarity(json_data.getString("clarity"));
user.setService(json_data.getString("service"));
user.setPolish(json_data.getString("polish"));
user.setSymetric(json_data.getString("symetric"));
user.setTables(json_data.getString("tables"));
user.setMeasurements(json_data.getString("measurments"));
user.setFlourscne(json_data.getString("flourscne"));
user.setDescription(json_data.getString("description"));
user.setCerticated(json_data.getString("certificated"));
user.setCcode(json_data.getString("ccode"));
user.setCut(json_data.getString("cut"));
user.setTotal(json_data.getString("total"));
user.setFile(json_data.getString("file"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Product code");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView shapes = new TextView(this);
shapes.setText("Shapes");
shapes.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
shapes.setPadding(5, 5, 5, 5);
shapes.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(shapes,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView pair = new TextView(this);
pair.setText("Shapes");
pair.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
pair.setPadding(5, 5, 5, 5);
pair.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(pair,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView flourscne = new TextView(this);
flourscne.setText("flourscne");
flourscne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
flourscne.setPadding(5, 5, 5, 5);
flourscne.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(flourscne,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView description = new TextView(this);
description.setText("description");
description.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
description.setPadding(5, 5, 5, 5);
description.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(description,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView certificated = new TextView(this);
certificated.setText("certificated");
certificated.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
certificated.setPadding(5, 5, 5, 5);
certificated.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(certificated,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView ccode = new TextView(this);
ccode.setText("ccode");
ccode.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ccode.setPadding(5, 5, 5, 5);
ccode.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(ccode,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView cut = new TextView(this);
cut.setText("cut");
cut.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
cut.setPadding(5, 5, 5, 5);
cut.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(cut,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView total = new TextView(this);
total.setText("total");
total.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
total.setPadding(5, 5, 5, 5);
total.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(total,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView file = new TextView(this);
file.setText("file");
file.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
file.setPadding(5, 5, 5, 5);
file.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(file,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
#SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users_availability> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users_availability p = (Users_availability) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getproduct_code());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getShapes());
place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
place.setPadding(5, 5, 5, 5);
place.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
label = new TextView(this);
label.setText(p.getCut());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
label = new TextView(this);
label.setText(p.getTotal());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
label = new TextView(this);
label.setText(p.getFile());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
Button btn = new Button(this);
btn.setText("Add to Basket");
// btn.setTextSize();
btn.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
// btn.setTag(mLinks.get(index));
btn.setOnClickListener(this);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
params.width=400;
params.height=60;
// btn.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
Ll.addView(btn,params);
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}
}
public void onClick(View v)
{
// startActivity(f1);
}
}
this is the class which contains the JSON parsing code
public class GetDatafromDB_Availability {
public String getDataFromDB() {
HttpPost httppost;
HttpClient httpclient;
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.0.106/test/availability.php");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response;
try {
response = httpclient.execute(httppost, responseHandler);
} catch (IOException e) {
e.printStackTrace();
System.out.println("ERROR : " + e.getMessage());
return "error";
}
return response.trim();
}
}
You should use AsyncTask for heavy-duty loadings such as http requests or parsing. Don't run such tasks on the ui thread, they will freeze it.
In the AsyncTask implementation's onPostExecute method you have the response you can deal with, populate your gui elements, update the views, etc.
A few references that may help you on how to proceed:
http://developer.android.com/reference/android/os/AsyncTask.html
http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/
http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.U3Rd98cRamE
I have this code, but it give me the error:
Error parsing data org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONArray.
It doesn't display the data when I run it. Pls Help me. Thks in advance
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
final GetDataFromDB getdb = new GetDataFromDB();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
#Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setName(json_data.getString("name"));
user.setPlace(json_data.getString("place"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("User");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText("Friend Of:");
place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
place.setPadding(5, 5, 5, 5);
place.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
#SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getName());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getPlace());
place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
place.setPadding(5, 5, 5, 5);
place.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
in AndroidManifest.xml add this
<uses-permission android:name="android.permission.INTERNET"/>