Dynamic Android Table - android

I ran into another problem. Found a tutorial on how to create a dynamic table,followed it but mine doesnt seems to work,when adding the dynamic rows. The static column headings works fine.
public class Leaders extends Activity {
TableLayout tl;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.leaderboard);
init();
}
#SuppressWarnings("deprecation")
private void init() {
tl = (TableLayout) findViewById(R.id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.RED);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_name = new TextView(this);
label_name .setId(20);
label_name .setText("NAME");
label_name .setTextColor(Color.WHITE);
label_name .setPadding(5, 5, 5, 5);
tr_head.addView(label_name );// add the column to the table row here
TextView label_predictions = new TextView(this);
label_predictions.setId(22);// define id that must be unique
label_predictions.setText("PREDICTIONS"); // set the text for the header
label_predictions.setTextColor(Color.WHITE); // set the color
label_predictions.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_predictions); // add the column to the table row here
TextView label_crrect = new TextView(this);
label_crrect.setId(23);// define id that must be unique
label_crrect.setText("Correct Predictions"); // set the text for the header
label_crrect.setTextColor(Color.WHITE); // set the color
label_crrect.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_crrect); // add the column to the table row here
TextView label_points = new TextView(this);
label_points.setId(21);// define id that must be unique
label_points.setText("Points"); // set the text for the header
label_points.setTextColor(Color.WHITE); // set the color
label_points.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_points); // add the column to the table row here
tl.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// TODO Auto-generated method stub
AsyncHttpClient getleaders = new AsyncHttpClient();
getleaders.get("http://10.0.2.2/fanaticmobile/leaders.php", new AsyncHttpResponseHandler(){
#Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1, arg2, arg3);
Log.d("error", arg3.toString());
}
#Override
public void onStart() {
mProgressDialog = ProgressDialog.show(Leaders.this, "Loading...", "Loading Data...");
// TODO Auto-generated method stub
super.onStart();
}
#Override
public void onFinish() {
mProgressDialog.dismiss();
// TODO Auto-generated method stub
super.onFinish();
}
#Override
#Deprecated
public void onSuccess(String content) {
// TODO Auto-generated method stub
super.onSuccess(content);
try {
JSONObject json = new JSONObject(content);
JSONArray leaders= json.getJSONArray("rows");
//Log.d("leaders",leaders.toString());
for(int i=0;i<leaders.length(); i++){
JSONObject jsonas = leaders.getJSONObject(i);
String fname = jsonas.getString("Fname");
String lname= jsonas.getString("Lname");
String predictions = jsonas.getString("Predictions");
String cp = jsonas.getString("Cpredictions");
String points = jsonas.getString("Points");
Integer count=0;
TableRow tr = new TableRow(this);
if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
tr.setId(100+count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView name = new TextView(this);
name.setId(200+count);
name.setText(fname+ " " + lname);
name.setPadding(2, 0, 5, 0);
name.setTextColor(Color.WHITE);
tr.addView(name);
//second row
TextView prdiction_lbl = new TextView(this);
prdiction_lbl.setId(200+count);
prdiction_lbl.setText(predictions);
prdiction_lbl.setPadding(2, 0, 5, 0);
prdiction_lbl.setTextColor(Color.WHITE);
tr.addView(prdiction_lbl);
//3rd row
TextView c_predi = new TextView(this);
c_predi.setId(200+count);
c_predi.setText(cp);
c_predi.setPadding(2, 0, 5, 0);
c_predi.setTextColor(Color.WHITE);
tr.addView(c_predi);
//4th
TextView points_lbl = new TextView(this);
points_lbl.setId(200+count);
points_lbl.setText(points);
points_lbl.setPadding(2, 0, 5, 0);
points_lbl.setTextColor(Color.WHITE);
tr.addView(points_lbl);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
i get this error in my eclipse "The constructor TableRow(new AsyncHttpResponseHandler(){}) is undefined". Please guide me.

TableRow requires context as an input param
But, when inside
AsyncHttpClient getleaders = new AsyncHttpClient();
getleaders.get("http://10.0.2.2/fanaticmobile/leaders.php", new AsyncHttpResponseHandler(){
......
}
the current this means object of AsyncHttpResponseHandler.. since you created Anonymous Inner Class of AsyncHttpResponseHandler
try
TableRow tr = new TableRow(Leaders.this);
instead.
This also goes without saying all instance where context is required you have to pass yourActivity.this , in your case Leaders.this..

Change TableRow tr = new TableRow(this); to TableRow tr = new TableRow(Leaders.this); in your AsyncHttpResponseHandler

Related

how to solve android.os.NetworkOnMainThreadException?

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.

Adding views programmatically. displays nothing

I am adding a view to my table row programmatically. Everything is working fine and logcat isn't showing any errors. Strangely enough nothing is being displayed in my activity.
Below is the code I am using:
try{
int k=0;
for(i=0;i<=Math.ceil(jArray.length()/2);i++){
Log.e("I...",""+i+" "+Math.ceil(jArray.length()/2));
tr_head = new TableRow(getApplicationContext());
tr_head.setId(10+i);
tr_head.setPadding(30, 30, 30, 5);
tr_head.setGravity(Gravity.LEFT|Gravity.CENTER_HORIZONTAL);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(j=0;j<2;j++)
{
if (k<jArray.length()){
jobj1 = jArray.getJSONObject(k);
LinearLayout parent = new LinearLayout(getApplicationContext());
parent.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(150), dpToPx(150)));
parent.setOrientation(LinearLayout.VERTICAL);
parent.setGravity(Gravity.CENTER);
ImageView img = new ImageView(getApplicationContext());
img.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(60),dpToPx(60)));
Picasso.with(getApplicationContext()).load(jobj1.getString("image")).resize(50,50).into(img);
parent.addView(img);
TextView txt = new TextView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
txt.setLayoutParams(params);
txt.setText(jobj1.getString("name"));
parent.addView(txt);
tr_head.addView(parent);
parent.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
String catid=jobj1.getString("category_id");
Intent intent = new Intent(getApplicationContext(), DescribeComplain.class);
intent.putExtra("category_id",catid);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
catch(JSONException je){
je.printStackTrace();
}
}
});
}
else{
break;
}
k++;
}
t1.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
}
catch(JSONException je){
je.printStackTrace();
}
}
});
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
After trying loads of methods, I solved it by setting linearLayout's parameters like this
LinearLayout parent = new LinearLayout(SelectCaegory.this);
TableRow.LayoutParams Paramslinear = new TableRow.LayoutParams(dpToPx(150),dpToPx(150));
Your problem is with this line:
LinearLayout parent = new LinearLayout(getApplicationContext());
You are creating a new view but she's not attach to your root view.
Try to declare your LinearLayout on your layout file, and fint it with the ID
LinearLayout parent = (LinearLayout) findViewById(R.id.my_parent_layout);

final LayoutParams size=new LayoutParams(200, 25) is not working when i apply this on my EditTexts

when i pass "size" as argument in code line it show no work......
Here is my code
final LayoutParams size=new LayoutParams(200, 25);//globally declared
private void Add() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
TableLayout myTable = new TableLayout(AddParing.this);
EditText[][] myEditTexts = new EditText[2][2];
myTable.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow[] myRow = new TableRow[2];
for (int a = 0; a < 1; a++) {
myRow[a] = new TableRow(AddParing.this);
myRow[a].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int b = 0; b < 2; b++) {
myEditTexts[a][b] = new EditText(AddParing.this);
myEditTexts[a][b].setText("");
myEditTexts[a[b].setBackgroundResource(R.drawable.input_bar);
myEditTexts[a][b].setLayoutParams(size);
//myEditTexts[a][b].setHeight(15);
myRow[a].setGravity(Gravity.CENTER);
myRow[a].addView(myEditTexts[a][b]);
}
myTable.addView(myRow[a]);
}
TableLayout lin = (TableLayout) findViewById(R.id.tableLayout1);
lin.addView(myTable);
}
});
i want to apply the setlayoutparams(). so how can i apply so it works correctly?
final TableRow.LayoutParams size=new TableRow.LayoutParams(200, 25);
use this code it works.

get id/tag from dynamically generated seekbar

In my app the user chooses how many seekbars they need and then my code generates them (see below).
// dynamically creates the view objects
tL = (TableLayout)findViewById(R.id.tableLayout1);
// creates all the fields
for(int i = 1; i <= numOfStockTanks; i++) {
TableRow tR = new TableRow(this);
// creates the textView
tV1 = new TextView(this);
tV1.setText(" " + tankNum[i - 1] + ": ");
tV1.setPadding(2, 0, 0, 0);
// creates the seekBar
sBGauge = new SeekBar(this);
sBGauge.setMax(depthL - 1);
sBGauge.setMinimumWidth(150);
sBGauge.setId(2000 + 1);
sBGauge.setOnSeekBarChangeListener(this);
// shows the progress of the seekBar
tVGauge = new TextView(this);
tVGauge.setText("0-0.0\"");
tVGauge.setId(3000 + i);
// adds objects to row
tR.addView(tV1);
tR.addView(sBGauge);
tR.addView(tVGauge);
// add the TableRow to the TableLayout
tL.addView(tR, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// creates new tableRow
TableRow tR2 = new TableRow(this);
// add's a "+" button to tableRow
buttonPlus = new Button (this);
buttonPlus.setWidth(60);
buttonPlus.setText("+");
buttonPlus.setId(4000 + i);
buttonPlus.setOnClickListener(new MyPOnClickListener());
// add's a "-" button to tableRow
buttonMinus = new Button (this);
buttonMinus.setWidth(60);
buttonMinus.setText("-");
buttonMinus.setId(5000 + i);
buttonMinus.setOnClickListener(new MyMOnClickListener());
// add TextView tVChange to show change from previous sqlite entry
tVChange = new TextView (this);
tVChange.setText("Change");
tVChange.setId(6000 + i);
// add the TextView and the buttons to the new TableRow
tR2.addView(buttonPlus);
tR2.addView(buttonMinus);
tR2.addView(tVChange);
// add the TableRow to the TableLayout
tL.addView(tR2,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} // end for statement
my problem is when I use my onProgressChanged method:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
int seekId = sBGauge.getId();
seekId = seekId - 2000;
try {
tVGauge = (TextView) findViewById(seekId + 3000);
} // end try
catch (Exception e) {Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();}
try {
// sets the text of the textview
tVGauge.setText(depth.get(progress));
} // end try
catch (Exception e) {}
manualP = progress;
}
My problem is that all my seekBars only change the first progress bars textView, the buttons change the proper text view.
Thanks for your help
You are setting the same id to each seekbar
sBGauge.setId(2000 + 1);
change 1 to i
sBGauge.setId(2000 + i);

Getting java.util.concurrent.RejectedExecutionException from asyncTask on android

I am reading a sqlite database into a tableLayout. I wan't to do this in a seperate thread instead of having a long wait with no ui updates. So I used an AsyncTask to do some of the work and publish the results. However only about 1/4th of the item in my list actually make it on to the TableLayout. It works fine without the AsyncTask. Most of the items on the list throw an error (which I caught) java.util.concurrent.RejectedExecutionException. I'm not sure why this is. Here is my code.
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);
c.moveToFirst();
if (c != null) {
int color = 0xFFdfe8ea;
this.startManagingCursor(c);
// Loop through all Results
do {
try{
MyAsyncTask aTask = new MyAsyncTask();
String[]strings= {c.getString(c.getColumnIndex("title")),c.getString(c.getColumnIndex("artist")),c.getString(c.getColumnIndex("time")),c.getString(c.getColumnIndex("album")),""+color};
aTask.execute(strings);
}catch(Exception e){
Log.w("****", e);
}
if (color == 0xFFdfe8ea) {
color = 0xFFf2f8fa;
} else {
color = 0xFFdfe8ea;
}
} while (c.moveToNext());
}
} catch (SQLException e) {
Log.e("****", e.toString());
} finally {
if (myDB != null) {
myDB.close();
}
}
and here is the AsyncTask
class MyAsyncTask extends AsyncTask<String, Void, View> {
#Override
protected View doInBackground(String... params) {
int color = Integer.parseInt(params[4]);
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView space = new TextView(MainActivity.this);
space.setText("");
space.setBackgroundColor(color); //0xFFf2f8fa alternating
space.setSingleLine();
space.setPadding(2, 2, 2, 2);
space.setGravity(Gravity.LEFT);
space.setTextColor(0xFF000000);
space.setLayoutParams(new LayoutParams(
findViewById(R.id.spaceColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView title = new TextView(MainActivity.this);
title.setText(params[0]);
title.setBackgroundColor(color); //0xFFf2f8fa alternating
title.setSingleLine();
title.setPadding(2, 2, 2, 2);
title.setGravity(Gravity.LEFT);
title.setTextColor(0xFF000000);
title.setEllipsize(TruncateAt.END);
title.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView artist = new TextView(MainActivity.this);
artist.setText(params[1]);
artist.setBackgroundColor(color); //0xFFf2f8fa alternating
artist.setSingleLine();
artist.setPadding(2, 2, 2, 2);
artist.setGravity(Gravity.LEFT);
artist.setTextColor(0xFF000000);
artist.setEllipsize(TruncateAt.END);
artist.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView time = new TextView(MainActivity.this);
time.setText(params[2]);
time.setBackgroundColor(color); //0xFFf2f8fa alternating
time.setSingleLine();
time.setPadding(2, 2, 2, 2);
time.setGravity(Gravity.LEFT);
time.setTextColor(0xFF000000);
time.setLayoutParams(new LayoutParams(
findViewById(R.id.timeColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView album = new TextView(MainActivity.this);
album.setText(params[3]);
album.setBackgroundColor(color); //0xFFf2f8fa alternating
album.setSingleLine();
album.setPadding(2, 2, 2, 2);
album.setGravity(Gravity.LEFT);
album.setTextColor(0xFF000000);
album.setEllipsize(TruncateAt.END);
album.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Add Button to row. */
tr.addView(space);
tr.addView(title);
tr.addView(artist);
tr.addView(time);
tr.addView(album);
/* Add row to TableLayout. */
return tr;
}
#Override
protected void onPostExecute(View tr) {
((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
#Override
protected void onPreExecute() {
}
}
For reference this is how I fixed it.
class MyAsyncTask extends AsyncTask<Void, Song, Void> {
#Override
protected Void doInBackground(Void... params) {
SQLiteDatabase myDB = openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
String TableName = "songs";
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);
c.moveToFirst();
int filepathIndex=c.getColumnIndex("filepath");
int titleIndex=c.getColumnIndex("title");
int artistIndex=c.getColumnIndex("artist");
int albumIndex=c.getColumnIndex("album");
int timeIndex=c.getColumnIndex("time");
int playcountIndex=c.getColumnIndex("playcount");
if (c != null) {
int color = 0xFFdfe8ea;
// this.startManagingCursor(c);
// Loop through all Results
do {
Song song = new Song(c.getString(filepathIndex),c.getString(titleIndex),c.getString(artistIndex),c.getString(albumIndex),c.getString(timeIndex),c.getInt(playcountIndex),color);
// Add to song the data from your cursor
publishProgress(song);
if (color == 0xFFdfe8ea) {
color = 0xFFf2f8fa;
} else {
color = 0xFFdfe8ea;
}
} while (c.moveToNext());
}
return null;
}
#Override
protected void onPostExecute(Void item) {
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Song... items) {
for (Song song : items) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView space = new TextView(MainActivity.this);
space.setText("");
space.setBackgroundColor(song.color); //0xFFf2f8fa alternating
space.setSingleLine();
space.setPadding(2, 2, 2, 2);
space.setGravity(Gravity.LEFT);
space.setTextColor(0xFF000000);
space.setLayoutParams(new LayoutParams(
findViewById(R.id.spaceColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView title = new TextView(MainActivity.this);
title.setText(song.title);
title.setBackgroundColor(song.color); //0xFFf2f8fa alternating
title.setSingleLine();
title.setPadding(2, 2, 2, 2);
title.setGravity(Gravity.LEFT);
title.setTextColor(0xFF000000);
title.setEllipsize(TruncateAt.END);
title.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView artist = new TextView(MainActivity.this);
artist.setText(song.artist);
artist.setBackgroundColor(song.color); //0xFFf2f8fa alternating
artist.setSingleLine();
artist.setPadding(2, 2, 2, 2);
artist.setGravity(Gravity.LEFT);
artist.setTextColor(0xFF000000);
artist.setEllipsize(TruncateAt.END);
artist.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView time = new TextView(MainActivity.this);
time.setText(song.time);
time.setBackgroundColor(song.color); //0xFFf2f8fa alternating
time.setSingleLine();
time.setPadding(2, 2, 2, 2);
time.setGravity(Gravity.LEFT);
time.setTextColor(0xFF000000);
time.setLayoutParams(new LayoutParams(
findViewById(R.id.timeColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView album = new TextView(MainActivity.this);
album.setText(song.album);
album.setBackgroundColor(song.color); //0xFFf2f8fa alternating
album.setSingleLine();
album.setPadding(2, 2, 2, 2);
album.setGravity(Gravity.LEFT);
album.setTextColor(0xFF000000);
album.setEllipsize(TruncateAt.END);
album.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Add Button to row. */
tr.addView(space);
tr.addView(title);
tr.addView(artist);
tr.addView(time);
tr.addView(album);
// Add the row to the table
((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
The reason you are seeing this RejectedExceutionException is almost certainly because you are submitting too many requests.
I just went into the code of AsyncTask and I noticed:
private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;
private static final int KEEP_ALIVE = 1;
private static final BlockingQueue<Runnable> sPoolWorkQueue =
new LinkedBlockingQueue<Runnable>(10);
/**
* An {#link Executor} that can be used to execute tasks in parallel.
*/
public static final Executor THREAD_POOL_EXECUTOR
= new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
That will build a bounded LinkedBlockingQueue. Up to 10 elements for the bound. The
MAXIMUM_POOL_SIZE I saw was 128 (which means if needed the Executor will create 128 threads at most).
Once you exceed the 128 thread and submit to a new MyTask instance with a queue depth of 10 you will get a RejectedExecutionException. This exception is thrown when you saturate all available threads and there is no more room on the queue.
You can easily confirm this by getting a thread-dump when the RejectedExecution occurs.
Basically, you can submit 138 MyTask's at any particular time but once you submit 139+ at the same time (not within the life time of the app) you will run into this issue
Edit: I went through the code more, and the most recent version (actually since Jan 16, 2011) this error should never happen.
With any version older then that you will run into this issue.
In short, if you upgrade you version this issue will go away, however each task will be executed serially and not concurrently.
If you want to do it using an AsyncTask, consider using publishProgress(), that way each item will be added as it is fetched from the database. This way:
NOTE: Consider that Song is a class with name, album, artist and time attributes.
class MyAsyncTask extends AsyncTask<Void, Song, Void> {
#Override
protected Void doInBackground(Void... params) {
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);
c.moveToFirst();
if (c != null) {
int color = 0xFFdfe8ea;
this.startManagingCursor(c);
// Loop through all Results
do {
Song song = new Song();
// Add to song the data from your cursor
publishProgress(song);
} while (c.moveToNext());
}
return null;
}
#Override
protected void onPostExecute(Void item) {
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Song... items) {
for (Song song : items) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView space = new TextView(MainActivity.this);
space.setText("");
space.setBackgroundColor(color); //0xFFf2f8fa alternating
space.setSingleLine();
space.setPadding(2, 2, 2, 2);
space.setGravity(Gravity.LEFT);
space.setTextColor(0xFF000000);
space.setLayoutParams(new LayoutParams(
findViewById(R.id.spaceColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView title = new TextView(MainActivity.this);
title.setText(song.getTitle());
title.setBackgroundColor(color); //0xFFf2f8fa alternating
title.setSingleLine();
title.setPadding(2, 2, 2, 2);
title.setGravity(Gravity.LEFT);
title.setTextColor(0xFF000000);
title.setEllipsize(TruncateAt.END);
title.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView artist = new TextView(MainActivity.this);
artist.setText(song.getArtist());
artist.setBackgroundColor(color); //0xFFf2f8fa alternating
artist.setSingleLine();
artist.setPadding(2, 2, 2, 2);
artist.setGravity(Gravity.LEFT);
artist.setTextColor(0xFF000000);
artist.setEllipsize(TruncateAt.END);
artist.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Create a Button to be the row-content. */
TextView time = new TextView(MainActivity.this);
time.setText(song.getTime());
time.setBackgroundColor(color); //0xFFf2f8fa alternating
time.setSingleLine();
time.setPadding(2, 2, 2, 2);
time.setGravity(Gravity.LEFT);
time.setTextColor(0xFF000000);
time.setLayoutParams(new LayoutParams(
findViewById(R.id.timeColumn).getWidth(),
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView album = new TextView(MainActivity.this);
album.setText(song.getAlbum());
album.setBackgroundColor(color); //0xFFf2f8fa alternating
album.setSingleLine();
album.setPadding(2, 2, 2, 2);
album.setGravity(Gravity.LEFT);
album.setTextColor(0xFF000000);
album.setEllipsize(TruncateAt.END);
album.setLayoutParams(new LayoutParams(
0,
LayoutParams.WRAP_CONTENT, 1));
/* Add Button to row. */
tr.addView(space);
tr.addView(title);
tr.addView(artist);
tr.addView(time);
tr.addView(album);
// Add the row to the table
((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
I believe you understood the concept behind AsyncTask wrongly, I strongly suggest you reread its documentation at Android Developers as its concept is a bit tricky to understand but very powerfull when you do so. As Romain Guy commented on your answer you can only execute UI code on the onPreExecute(), onProgressUpdate() and onPostExecute() methods.
I dont think you need to create one AsyncTask for that. You are not fetching anything from the network or downloading images. Its just standard loading.
I would limit the result in the SQL with 'limit'.
Also, you are doing that inside an adapter right? Because I think you are adding everything to the list, where you should create a listview in your layout and set an Adapter. Maybe extending BaseAdapter.
Every adapter has a convenient method called getView that will get called only when it is visible and should help with your problem.
This is an example of an adapter:
public class MyAdapter extends BaseAdapter {
private Context context = null;
private Cursor cursor;
public MyAdapter(Context context){
this.context = context;
SQLiteDatabase db = DatabaseHelper.getInstance(context).getReadableDatabase();
this.cursor = db.query("YOUR QUERY");
}
#Override
public int getCount() {
return this.cursor.getCount();
}
public Cursor getCursor() {
return cursor;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout row;
try {
cursor.moveToPosition(position);
if (convertView == null) {
row = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.myRowLayout, parent, false);
} else {
row = (LinearLayout) convertView;
}
TextView name = (TextView) row.findViewById(R.id.myLayoutId);
name.setText(cursor.getString(cursor.getColumnIndex("your column")));
} catch (Exception e) {
row = null;
Log.e(LOG_TAG, "" + e.getMessage());
e.printStackTrace();
}
return row;
}
#Override
public MoneyCurrency getItem(int position) {
this.cursor.moveToPosition(position);
long id = this.cursor.getLong(this.cursor.getColumnIndex("your column id"));
return new Object.read(id, context, null); //Return whatever you want to show in that row. This is used if you want to use onClick listeners or menus
}
#Override
public long getItemId(int position) {
this.cursor.moveToPosition(position);
return this.cursor.getLong(this.cursor.getColumnIndex("your id column"));
}
}

Categories

Resources