0

I am setting my textview using columns from a sqlite database file.Problem is that when i change the view back and forth, there is a very obvious lag while it retrieves from the database.Here is the code:

public class Flipcard extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout layMain = (LinearLayout) findViewById(R.id.FlashCardFront);
    layMain.setOnClickListener(this);
    Cursor cur;
    DbH db = null;
    TextView tv=(TextView) findViewById(R.id.TV_Word);
    TextView txtView=(TextView) findViewById(R.id.TV_CardNo);
    try {
        db=new DbH(this);
    } catch (IOException e2) {

        e2.printStackTrace();
    }
    try {
        db.createdatabase();
    } catch (IOException e) {

        e.printStackTrace();
    }
    db.opendatabase();
    cur=db.data();
    cur.moveToFirst();
    tv.setText(cur.getString(0));
    cur.close();
}
public void onClick(View v) {
Intent i=new Intent(this,Flipcard_back.class);
startActivity(i);
}

}

the other java file that is being called on click is:

 public class Flipcard_back extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_back);
    LinearLayout layMain = (LinearLayout) findViewById(R.id.FlashCardRear);
    layMain.setOnClickListener(this);
}
public void onClick(View v) {
Intent i=new Intent(this,Flipcard.class);
startActivity(i);
}
}

which is actually calling previous one.The problem is i'm one a very initial phase on my app development.Right now i am only trying to change two textviews in first java file using my database.When i switch back and forth there is a lag of around a second.How do i correct it? am i doing something wrong. Also i am not able to retrieve all the rows using getstring(1) etc.Why so?How do i get the columns one by one?

Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • Edit: Logcat shows following every time i switch back and forth: need to grow: mSize=1048576, size=70, freeSpace()=26, numRows=2736 not growing since there are already 2736 row(s), max size=1048576 failed allocating fieldDir at startPos 0row 2736. what does this mean? – Maxsteel Apr 21 '12 at 21:48
  • Please follow this example of a [detailed logcat.](http://stackoverflow.com/questions/10263248/android-unable-to-instantiate-activity-classnotfound-fails-on-one-eclipse) – Sam Apr 21 '12 at 22:03

2 Answers2

1

The lag is caused by accessing your database in your main thread, you want to have it run in its own thread. Here is a the Developer's Guide on Processes and Thread which will give you the grand overview. Look into something like AsyncTask next, for one approach to accessing databases.

As for not getting results from your database, I cannot tell without seeing the class associated with db. Include that code and any logcat errors in your question if you want more help.

EDIT

With the error code you provided I found this link to a similar question and detailed answer.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
0

My experience from sqlite + android is that its initially very slow when quering the database, but after that it can retrieve large amounts of data very fast. If you want to avoid lag, use a file instead. My tiny experience comes from developing the dictionary app Wordlist Pro where I had to abandon the database for textfiles since it was way too slow (and big in size).

ernell
  • 356
  • 3
  • 5