In my doinBackground() function of Asynctask I am inserting heavy data in my database. At the same time I want to scroll list on Activity. But when doinBackground execute, it freeze or hang my list. At that time I am not able to scroll the list.I struct on this fromlast 2 days. I have tried a lot for this, but didn't get any clear description and reason. So, plzz if there is any IDEA that how to resolve it, will be very appreciable. Thanks in advance.
int i =0;
for(int j = i; j
for(int k = j; k<Integer.parseInt(listchscheduleArraylength.get(j));k++){
if(databasehelper.insert_into_table2(list_ch_autoid.get(j), list_sch_prg_id.get(k), list_sch_prg_title.get(k)
, list_sch_prg_gnre.get(k), list_sch_prg_sch_id.get(k), list_sch_prg_strt_time.get(k),
list_sch_prg_end_time.get(k), list_sch_prg_sch_date.get(k), list_sch_prg_sch_desc.get(k))){
Log.v("TABLE 2 status", "inserted");
}else {
Log.v("TABLE 2 status", " not inserted");
}
}// End of inner loop
} // End of outer loop
Related
In my android program, I have 3 functions that will be continuously repeated until the user exits the app. Currently, it is able to update a value(position) every second.
I was wondering if having lots of if else condition in one of the function will slow down the performance of my app i.e. it might take way longer than a second for the updates to kick in.
Note that 2 of the if else function will execute only once. All the if-else function have very little code in it. Below is the code
public void positionUpdated(Coordinate userPosition, int accuracy) {
// GETS EXECUTED ONCE //
if(nameList.size() == 0) {nameList = indoorsFragment.getZones();}
if(end == null) {
for(int i=0 ; i < nameList.size() ; i++) if(nameList.get(i).equals(name)) end = nameList.get(i).getZonePoints().get(0);
}
// GETS EXECUTED ONCE //
if(Math.abs(userPosition.x - end.x) >500) {Toast.makeText(this,"WALK " + Math.abs(end.x - userPosition.x)/1000 + " Meters" , Toast.LENGTH_SHORT).show();return;}
if(turn) {
if ((userPosition.y - end.y) < 0) {Toast.makeText(this, "STOP AND TURN RIGHT", Toast.LENGTH_LONG).show();turn=false;return;}
if ((userPosition.y - end.y) > 0) {Toast.makeText(this, "STOP AND TURN LEFT" , Toast.LENGTH_LONG).show();turn=false;return;}
}
if((Math.abs(userPosition.y - end.y) < 500)) Toast.makeText(this, "WALK", Toast.LENGTH_LONG).show();
}
Hope to receive some feedback regarding this , thanks
Haziq
The number of statements doesn't really matter, in fact they're executed in fractions of nanoseconds. what matters is if you have many nested loops or recursions.
If you don't want much work to be done in the foreground thread, use an AsyncTask or Runnable then update the UI in the main thread.
I have a ListView. This ListView load this text/data from a URL/HTML code on a webpage. I use a for loop for it like:
for (int i = 0; i < 5; j++) {
// Search and load text in the ListView..
}
But sometimes the webpage has 5 "textfields" but maybe a new post got 8..
So, I don't want to use the 5 in the for loop anymore.. I want a for loop which is loading and loading untill he find a specific line in the html code of the webpage.
For example:
if (MaxLoad != "<p>End of the textfields</p>") {
// Search and load text in the ListView,
// untill the found text is the text between the "".
}
}
else{
Log.e("Max textfields are found!")
}
Sometimes he need to stop after 3 textfields.. But another time he need to stop after 16 textfields..
I hope I was clear enough.
Thanks,
P.S. All my code is working at the moment.. When I use the for loop system, count the textfields in the HTML manually.. Put that value into the for loop, then the code load all the textfields.. But I want it automaticly..
Use the break; statement to break out of your for loop. You can initiate the for loop with a big number like 5000.
for (int i = 0; i < 5000; j++) {
// Search and load text in the ListView..
String ItemText = .......
if ( ItemText.equals ( "blablabla" ) )
break;
}
This could be done more elegant though...
Calling the ORMLite RuntimeExceptionDao's createOrUpdate(...) method in my app is very slow.
I have a very simple object (Item) with a 2 ints (one is the generatedId), a String and a double. I test the time it takes (roughly) to update the object in the database (a 100 times) with the code below. The log statement logs:
time to update 1 row 100 times: 3069
Why does it take 3 seconds to update an object 100 times, in a table with only 1 row. Is this the normal ORMLite speed? If not, what might be the problem?
RuntimeExceptionDao<Item, Integer> dao =
DatabaseManager.getInstance().getHelper().getReadingStateDao();
Item item = new Item();
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
item.setViewMode(i);
dao.createOrUpdate(item);
}
long update = System.currentTimeMillis();
Log.v(TAG, "time to update 1 row 100 times: " + (update - start));
If I create 100 new rows then the speed is even slower.
Note: I am already using ormlite_config.txt. It logs "Loaded configuration for class ...Item" so this is not the problem.
Thanks.
This may be the "expected" speed unfortunately. Make sure you are using ORMLite version 4.39 or higher. createOrUpdate(...) was using a more expensive method to test for existing of the object in the database beforehand. But I suspect this is going to be a minimal speed improvement.
If I create 100 new rows then the speed is even slower.
By default Sqlite is in auto-commit mode. One thing to try is to wrap your inserts (or your createOrUpdates) using the the ORMLite Dao.callBatchTasks(...) method.
In by BulkInsertsTest android unit test, the following doInserts(...) method inserts 1000 items. When I just call it:
doInserts(dao);
It takes 7.3 seconds in my emulator. If I call using the callBatchTasks(...) method which wraps a transactions around the call in Android Sqlite:
dao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
doInserts(dao);
return null;
}
});
It takes 1.6 seconds. The same performance can be had by using the dao.setSavePoint(...) method. This starts a transaction but is not as good as the callBachTasks(...) method because you have to make sure you close your own transaction:
DatabaseConnection conn = dao.startThreadConnection();
Savepoint savePoint = null;
try {
savePoint = conn.setSavePoint(null);
doInserts(dao);
} finally {
// commit at the end
conn.commit(savePoint);
dao.endThreadConnection(conn);
}
This also takes ~1.7 seconds.
The loop below seems to stop short, and then restart. It is not inside of another loop. The first Log call prints 36, thus the outer for loop should run 36 times. The Log call inside of the loop though, which is meant to print the number of times the loop has run, prints "0" up to "4" meaning the loop only ran 5 times. Would there be any reason for this process to start over so that the first Log call fires again, and the loop again runs through only 5 times? This occurs twice according to my Logcat output.
ArrayList<RunData_L> rdUP = t.getMyPositiveRunData();
ArrayList<RunData_L> rdDOWN = t.getMyNegativeRunData();
Log.d("rdUP size", rdUP.get(0).getMyMeasurementData().size() + "");
for (int i = 0; i < rdUP.get(i).getMyMeasurementData().size(); i++) {
Log.d("i", i + "");
ArrayList<BigDecimal> tempUP = new ArrayList<BigDecimal>(), tempDOWN = new ArrayList<BigDecimal>();
for(int j = 0; j < rdUP.size(); j++) {
tempUP.add(rdUP.get(j).getMyMeasurementData().get(i));
tempDOWN.add(rdDOWN.get(j).getMyMeasurementData().get(i));
}
pdUP.add(tempUP);
pdDOWN.add(tempDOWN);
}
I suspect as per my comment that the use of rdUP.get(j) in the inner loop is causing the problem.
You first test to see the size of rdUP.get(i).getMyMeasurementData() in the outer loop as your bounding condition for the loop and so i runs from 0 to the size of rdUP.get(i).getMyMeasurementData().
Your inner loop then says go through each element of rdUP and get the i th value from rdUP.get(j).getMyMeasurementData(). How do you know that the j th rdUP has enough elements to satisfy your get(i) ?
I apologize for the limited title, but I don't know how to describe this problem exactly. I have a for loop that iterates through an arrayList containing an object. One of the objects methods is a boolean that is set at a different point in time. The point of the for loop is to go through the arrayList and remove each item that contains the boolean as false.
if(!arrayList.isEmpty()){
int len = arrayList.size();
for(int i=0; i<len; i++){
if(!arrayList.get(i).isStartTimer()){
arrayList.remove(i);
}
}
}
The for loop always leaves one too many objects in the array. This is because each time the for loop cycles the len drops down one because of the removed item in the array and on the last object it cancels the for loop before it can remove the object. I understand the problem, but I can't figure out how to fix it. I have tried doing something such as len+1 but it throws an indexOutOfBounds exception when I do anything to it. Any ideas? Also, is this the best way to do what I am trying to do?
you can switch to while:
if( ! arrayList.isEmpty() ){
int i = 0;
while( i < arrayList.size() ) {
if( ! arrayList.get(i).isStartTimer() ){
arrayList.remove(i);
continue; // skip i++
}
i++;
}
}
You should loop backwards so that you don't use indicies that come after the items you removed.
for (int i = arrayList.size() - 1; i >= 0; i--) {
}
Alternatively, you can just decrement i to use the next index, and get rid of the len variable so that the ending condition uses the actual length.