Reputation: 192
I am trying to retrieve the row_count of a table from SQLite. The code, which I used to do this task is freezing my application. This is entirely run on back-end, nothing is there in the MainUI thread. But, also I am getting ANR error.
This is my code, where I am calling the method -
public class del_person extends Activity{
String[] images = {};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.del_person);
final adapter info = new adapter(this);
// new del_person_Dup(null).execute();
new Task(info).execute((Void)null);
}
}
class Task extends AsyncTask<Void, Void, Void> {
String[] images = {};
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
Log.v("distinctrowcount() is:",""+mAdapter.distinctrowcount()+"");
Log.v("started del_person.java","started del_person.java");
for (int i=0;i<mAdapter.distinctrowcount();i++)
{
Log.v ("This is the "+i+"th iteration","This is the "+i+"th iteration");
images[i] =mAdapter.getPersonList(i+1);
Log.v("Persons",images[i]);
}
return null;
}
}
I also tried using AsyncTask for this. But also, I am unable to come out of this.
Here is my distinctrowcount() from adapter class:
adapter.java:
public int distinctrowcount()
{
int rc = 0;
try {
open();
rc = mDbHelper.getdistinctrc();
close();
}
catch (Exception e)
{
e.printStackTrace();
}
return rc;
}
helper.java - getdistinctrc():
public int getdistinctrc() {
// TODO Auto-generated method stub
SQLiteDatabase myDB;
int values = 0;
int distinct_count = 0;
try {
myDB=this.openDataBase();
Cursor c=myDB.rawQuery("select count (DISTINCT FileName) from Photos;",null);
if (c != null ) {
String h = "";
while(c.moveToFirst()){
distinct_count = c.getInt(0);
// h = c.getString(c.getColumnIndex("COUNT(*)"));
}
}
if(c != null)
{
c.close();
myDB.close();
}
}catch(SQLException sqle){
throw sqle;
}
return distinct_count;
}
AsyncTask code:
class Task extends AsyncTask<Void, Void, Void> {
String[] images = {};
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
Log.v("distinctrowcount() is:",""+mAdapter.distinctrowcount()+"");
Log.v("started del_person.java","started del_person.java");
for (int i=0;i<mAdapter.distinctrowcount();i++)
{
Log.v ("This is the "+i+"th iteration","This is the "+i+"th iteration");
images[i] =mAdapter.getPersonList(i+1);
Log.v("Persons",images[i]);
}
return null;
}
}
Is there any other better way to implement this, other than Runnable() and AsyncTask? Please help me with some sample code.
Thanks in advance.
Upvotes: 0
Views: 73
Reputation: 87064
Your current code doesn't run those database queries on a background thread. Use an AsyncTask
instead like this:
static class Task extends AsyncTask<Void, Void, Void> {
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
return null;
}
And in the Activity
's onCreate()
you'll use something like:
new Task(info).execute((Void)null);
Upvotes: 1