Related
public class DoLogin extends AsyncTask<String,String,String>
{
ResultSet rs2;
String z = "";
Boolean isSuccess = false;
TextView DateOfBooking,Product,CustomerName,
Quantity,Destination,DealerName,Remarks,DueDate;
ArrayList DateOfBooking1 = new ArrayList();
ArrayList Product1 = new ArrayList();
ArrayList CustomerName1 = new ArrayList();
ArrayList Quantity1 = new ArrayList();
ArrayList Destination1 = new ArrayList();
ArrayList DealerName1 = new ArrayList();
ArrayList Remarks1 = new ArrayList();
ArrayList DueDate1 = new ArrayList();
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String r) {
Toast.makeText(OrderRequest.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess) {
try {
addHeaders();
do{
s1 = rs2.getString(1);
DateOfBooking1.add(s1);
s2 = rs2.getString(2);
CustomerName1.add(s2);
s3 = rs2.getString(3);
Destination1.add(s3);
s4 = rs2.getString(4);
DealerName1.add(s4);
s5 = rs2.getString(5);
Product1.add(s5);
s6 = rs2.getString(6);
Quantity1.add(s6);
s7 =rs2.getString(7);
Remarks1.add(s7);
s8 =rs2.getString(8);
DueDate1.add(s8);
}while(rs2.next());
if (DateOfBooking1.size() != 0) {
for (int j = 0; j < DateOfBooking1.size(); j++) {
/** Create a TableRow dynamically **/
tr = new TableRow(OrderRequest.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
DateOfBooking = new TextView(OrderRequest.this);
DateOfBooking.setText(DateOfBooking1.get(j).toString());
DateOfBooking.setTextColor(Color.BLACK);
DateOfBooking.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DateOfBooking.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DateOfBooking.setPadding(5, 5, 5, 5);
DateOfBooking.setId(j);
tr.addView(DateOfBooking); // Adding textView to
tablerow.
CustomerName = new TextView(OrderRequest.this);
CustomerName.setText(CustomerName1.get(j).toString());
CustomerName.setTextColor(Color.BLACK);
CustomerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
CustomerName.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
CustomerName.setPadding(5, 5, 5, 5);
CustomerName.setId(j);
tr.addView(CustomerName); // Adding textView to
tablerow.
Destination = new TextView(OrderRequest.this);
Destination.setText(Destination1.get(j).toString());
Destination.setTextColor(Color.BLACK);
Destination.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Destination.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Destination.setPadding(5, 5, 5, 5);
Destination.setId(j);
tr.addView(Destination); // Adding textView to
tablerow.
DealerName = new TextView(OrderRequest.this);
DealerName.setText(DealerName1.get(j).toString());
DealerName.setTextColor(Color.BLACK);
DealerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DealerName.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DealerName.setPadding(5, 5, 5, 5);
DealerName.setId(j);
tr.addView(DealerName); // Adding textView to
tablerow.
Product = new TextView(OrderRequest.this);
Product.setText(Product1.get(j).toString());
Product.setTextColor(Color.BLACK);
Product.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Product.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Product.setPadding(5, 5, 5, 5);
Product.setId(j);
tr.addView(Product); // Adding textView to
tablerow.
Quantity = new TextView(OrderRequest.this);
Quantity.setText(Quantity1.get(j).toString());
Quantity.setTextColor(Color.BLACK);
Quantity.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Quantity.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Quantity.setPadding(5, 5, 5, 5);
Quantity.setId(j);
tr.addView(Quantity); // Adding textView to
tablerow.
Remarks = new TextView(OrderRequest.this);
Remarks.setText(Remarks1.get(j).toString());
Remarks.setTextColor(Color.BLACK);
Remarks.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Remarks.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Remarks.setPadding(5, 5, 5, 5);
Remarks.setId(j);
tr.addView(Remarks); // Adding textView to
tablerow.
DueDate = new TextView(OrderRequest.this);
DueDate.setText(DueDate1.get(j).toString());
DueDate.setTextColor(Color.BLACK);
DueDate.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DueDate.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DueDate.setPadding(5, 5, 5, 5);
DueDate.setId(j);
tr.addView(DueDate); // Adding textView to
tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
} else {
Toast.makeText(OrderRequest.this,
DateOfBooking1+""+Product1+""+DealerName1
+""+Destination1+""+DueDate1+""+CustomerName1
+""+Quantity1+""+Remarks1+"Sorry.....",
Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
Log.e("showing",e+"");
}
}
}
#Override
protected String doInBackground(String... params) {
try {
Connection con = (Connection) connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select
DocDate,CustomerName,Destination,DealerName,
ProductName,Quantity,Remarks,DueDate from [Dealer].[dbo].
[BookingOrder]";
Statement stmt = con.createStatement();
rs2 = stmt.executeQuery(query);
try {
if (rs2.next()) {
isSuccess = true;
z = "Successfully Viewed";
}
}catch (Exception n)
{
z = "selecting";
Log.e("selecting",n+"");
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
Log.e("Exc", ex + "");
return null;
}
return z;
}
}
I am getting exception as "android.os.NetworkOnMainThreadException",Resultset is giving me the values which are located in SQL server.but the code after getting values from resultset is not executing.How can I overcome this exception,Already I checked this exception,but the solutions are not working.Help me.Thank you in advance.
android.os.NetworkOnMainThreadException
is thrown when an application attempts to perform a network related operation on the main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher versions. Ensure that your application is not attempting to perform any network
related operation on its main thread.
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 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++;
}
}
I have a TableLayout and fill it with a WebService and it's Works fine, but when I want to execute the second time the WebService, the data show it below the previous, I have to clean before show a new information, how can do that?
The code onTaskComplete and showTablaPos(fill TableLayOut)
// Called after task finished
#Override
public void onTaskComplete(JSONObject result) {
// TODO Auto-generated method stub
try{
ljsonObject = result;
//Se obtienen los valores de status y mensaje
int status = ljsonObject.getInt("status");
String mensaje = ljsonObject.getString("mensaje");
mensaje = mensaje.concat(", favor de llamar a la oficina de tú cancha para reportar este mensaje, gracias");
//status = 0 para el Web Service de Obtener catálogo de Torneos, status = 1 para obtener la tabla de posiciones
try {
ljsonArray = ljsonObject.getJSONArray(tag);
//Arma y carga la tabla de Posiciones
showTablaPos();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
}
}
public void showTablaPos(){
TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
//Create table row header
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
//tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//Se crean los header
TextView label_equipo = new TextView(this);
label_equipo.setId(20);
label_equipo.setText("EQUIPO");
label_equipo.setTextColor(Color.WHITE);
label_equipo.setPadding(5, 5, 5, 5);
tr_head.addView(label_equipo);
TextView label_jj = new TextView(this);
label_jj.setId(20);
label_jj.setText("JJ");
label_jj.setTextColor(Color.WHITE);
label_jj.setPadding(5, 5, 5, 5);
tr_head.addView(label_jj);
TextView label_jg = new TextView(this);
label_jg.setId(20);
label_jg.setText("JG");
label_jg.setTextColor(Color.WHITE);
label_jg.setPadding(5, 5, 5, 5);
tr_head.addView(label_jg);
TextView label_je = new TextView(this);
label_je.setId(20);
label_je.setText("JE");
label_je.setTextColor(Color.WHITE);
label_je.setPadding(5, 5, 5, 5);
tr_head.addView(label_je);
TextView label_jp = new TextView(this);
label_jp.setId(20);
label_jp.setText("JP");
label_jp.setTextColor(Color.WHITE);
label_jp.setPadding(5, 5, 5, 5);
tr_head.addView(label_jp);
TextView label_gf = new TextView(this);
label_gf.setId(20);
label_gf.setText("GF");
label_gf.setTextColor(Color.WHITE);
label_gf.setPadding(5, 5, 5, 5);
tr_head.addView(label_gf);
TextView label_ge = new TextView(this);
label_ge.setId(20);
label_ge.setText("GE");
label_ge.setTextColor(Color.WHITE);
label_ge.setPadding(5, 5, 5, 5);
tr_head.addView(label_ge);
TextView label_dif = new TextView(this);
label_dif.setId(20);
label_dif.setText("DIF");
label_dif.setTextColor(Color.WHITE);
label_dif.setPadding(5, 5, 5, 5);
tr_head.addView(label_dif);
TextView label_ptos = new TextView(this);
label_ptos.setId(20);
label_ptos.setText("PTOS.");
label_ptos.setTextColor(Color.WHITE);
label_ptos.setPadding(5, 5, 5, 5);
tr_head.addView(label_ptos);
tablaPos.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Integer count = 0;
Integer cuantos = ljsonArray.length();
try{
for (int i = 0; i < ljsonArray.length(); i++) {
ljsonObject = ljsonArray.getJSONObject(i);
String equipo = (ljsonObject.optString("equ_nombre"));
Integer jj = (ljsonObject.optInt("jj"));
Integer jg = (ljsonObject.optInt("jg"));
Integer je = (ljsonObject.optInt("je"));
Integer jp = (ljsonObject.optInt("jp"));
Integer gf = (ljsonObject.optInt("gf"));
Integer ge = (ljsonObject.optInt("ge"));
Integer dif = (ljsonObject.optInt("dif"));
Integer pto = (ljsonObject.optInt("puntos"));
TableRow tr = new TableRow(this);
if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
tr.setId(100+count);
//tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//Create columns to add as table data
// Create a TextView to add equipo
TextView labelEquipo = new TextView(this);
labelEquipo.setId(200+count);
labelEquipo.setText(equipo);
labelEquipo.setPadding(2, 0, 5, 0);
labelEquipo.setTextColor(Color.WHITE);
tr.addView(labelEquipo);
TextView labelJj = new TextView(this);
labelJj.setId(200+count);
labelJj.setText(Integer.toString(jj));
labelJj.setPadding(2, 0, 5, 0);
labelJj.setTextColor(Color.WHITE);
labelJj.setGravity(Gravity.CENTER);
tr.addView(labelJj);
TextView labelJg = new TextView(this);
labelJg.setId(200+count);
labelJg.setText(Integer.toString(jg));
labelJg.setPadding(2, 0, 5, 0);
labelJg.setTextColor(Color.WHITE);
labelJg.setGravity(Gravity.CENTER);
tr.addView(labelJg);
TextView labelJe = new TextView(this);
labelJe.setId(200+count);
labelJe.setText(Integer.toString(je));
labelJe.setPadding(2, 0, 5, 0);
labelJe.setTextColor(Color.WHITE);
labelJe.setGravity(Gravity.CENTER);
tr.addView(labelJe);
TextView labelJp = new TextView(this);
labelJp.setId(200+count);
labelJp.setText(Integer.toString(jp));
labelJp.setPadding(2, 0, 5, 0);
labelJp.setTextColor(Color.WHITE);
labelJp.setGravity(Gravity.CENTER);
tr.addView(labelJp);
TextView labelGf = new TextView(this);
labelGf.setId(200+count);
labelGf.setText(Integer.toString(gf));
labelGf.setPadding(2, 0, 5, 0);
labelGf.setTextColor(Color.WHITE);
labelGf.setGravity(Gravity.CENTER);
tr.addView(labelGf);
TextView labelGe = new TextView(this);
labelGe.setId(200+count);
labelGe.setText(Integer.toString(ge));
labelGe.setPadding(2, 0, 5, 0);
labelGe.setTextColor(Color.WHITE);
labelGe.setGravity(Gravity.CENTER);
tr.addView(labelGe);
TextView labelDif = new TextView(this);
labelDif.setId(200+count);
labelDif.setText(Integer.toString(dif));
labelDif.setPadding(2, 0, 5, 0);
labelDif.setTextColor(Color.WHITE);
labelDif.setGravity(Gravity.CENTER);
tr.addView(labelDif);
TextView labelPtos = new TextView(this);
labelPtos.setId(200+count);
labelPtos.setText(Integer.toString(pto));
labelPtos.setPadding(2, 0, 5, 0);
labelPtos.setTextColor(Color.WHITE);
labelPtos.setGravity(Gravity.CENTER);
tr.addView(labelPtos);
// finally add this to the table row
tablaPos.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
count++;
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
}
try to call this function before fill data
private void deleteRows(){
TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
tablaPos.removeAllViews();
}
UPDATE:
try this
private void deleteRows(){
TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
for(int i = 0; i < tablaPos.getChildCount(); i++){
tablaPos.removeViewAt(i);
}
}
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"/>