0

我正在使用 sqlite 数据库文件中的列设置我的 textview。问题是当我来回更改视图时,从数据库中检索时存在非常明显的滞后。这是代码:

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);
}

}

单击时调用的另一个java文件是:

 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);
}
}

这实际上是在调用前一个。问题是我是我的应用程序开发的一个非常初始阶段。现在我只是尝试使用我的数据库更改第一个 java 文件中的两个文本视图。当我来回切换时有一个大约一秒钟的延迟。我该如何纠正?难道我做错了什么。此外,我无法使用 getstring(1) 等检索所有行。为什么会这样?如何一一获取列?

4

2 回答 2

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.

于 2012-04-21T21:15:16.553 回答
0

我从 sqlite + android 的经验是,它最初在查询数据库时非常慢,但之后它可以非常快速地检索大量数据。如果要避免延迟,请改用文件。我的小经验来自开发词典应用程序 Wordlist Pro,因为它太慢(而且体积很大),我不得不放弃数据库以获取文本文件。

于 2012-04-21T22:53:47.760 回答