英文:
android Skipped 44 frames! The application may be doing too much work on its main thread
问题
我正在使用Java和SQLite开发一款图书应用,我想在列表视图中显示内容列表。我已经解决了一些打开数据库的错误,但现在数据库已经打开,但是列表视图中没有显示任何内容,我收到了以下错误。
以下是我的活动代码。此活动在主活动中点击按钮后启动:
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class LVSwitches extends AppCompatActivity {
public static final String LOGTAG = "book";
private BookDataSource dataSource;
private List<season> seasons;
private ListView listView;
private ArrayAdapter<season> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l_v_switches);
listView = (ListView) findViewById(R.id.season_list);
dataSource = new BookDataSource(getBaseContext());
this.seasons = this.dataSource.getAllSeasons();
if (seasons.size() == 0) {
this.dataSource.CopyDbfile();
this.seasons = this.dataSource.getAllSeasons();
}
}
}
以下是数据源的代码:
public class BookDataSource {
// ... 其他部分未翻译 ...
public List<season> getAllSeasons() {
List<season> items = new ArrayList<season>();
openDb();
String[] columns = {bookDBHelper.KEY_ID, bookDBHelper.KEY_CONTENT, bookDBHelper.KEY_ISFAV};
Cursor cursor = database.query(bookDBHelper.TABLE_LVS, columns, bookDBHelper.KEY_TYPE + " = \"" + bookDBHelper.TYPE_INDX + "\"",
null, null, null, bookDBHelper.KEY_ID);
if (cursor.moveToFirst()) {
do {
season season = new season();
season.setId(cursor.getLong(cursor.getColumnIndex(bookDBHelper.KEY_ID)));
season.setStitle(cursor.getString(cursor.getColumnIndex(bookDBHelper.KEY_CONTENT)));
season.setFav(cursor.getInt(cursor.getColumnIndex(bookDBHelper.KEY_ISFAV)) > 0);
items.add(season);
} while (cursor.moveToNext());
}
return items;
}
// ... 其他部分未翻译 ...
}
season
类:
public class season {
// ... 其他部分未翻译 ...
}
请注意,因为你只需要翻译特定的代码部分,所以我已经排除了注释和其他无关的内容。
英文:
I am developing a book app using Java and SQLite and I want to show the list of the contents in a list view. I have already solved a few errors for opening the database but now the database opens but nothing is showing in the list view and I receive that error.
The code for my activity. This activity starts after clicking on a button in the main activity:
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class LVSwitches extends AppCompatActivity {
public static final String LOGTAG="book";
private BookDataSource dataSource;
private List<season> seasons;
private ListView listView;
private ArrayAdapter<season> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l_v_switches);
listView = (ListView) findViewById(R.id.season_list);
dataSource=new BookDataSource(getBaseContext());
this.seasons = this.dataSource.getAllSeasons();
//dataSource.getAllSeasons();
if (seasons.size()==0){
this.dataSource.CopyDbfile();
this.seasons = this.dataSource.getAllSeasons();
}
}
}
Here is the code for the data source
public class BookDataSource {
private Context context;
private SQLiteDatabase database;
private SQLiteOpenHelper dbhelper;
private static String DB_PATH = "";
private static String DB_NAME = "lvs";
public BookDataSource(Context context){
this.context=context;
this.dbhelper=new bookDBHelper(context);
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
public void openDb(){
database=dbhelper.getWritableDatabase();
String myPath = DB_PATH + DB_NAME;
database = SQLiteDatabase.openDatabase
(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
Log.i(LVSwitches.LOGTAG,"database opened");
}
public void closeDb(){
if (database!=null){
database.close();
//Log.i(LVSwitches.LOGTAG,"database closed");
}else {
Log.i(LVSwitches.LOGTAG,"db=null");
}
}
public List<season> getAllSeasons(){
List<season> items=new ArrayList<season>();
openDb();
String[] colums= {bookDBHelper.KEY_ID,bookDBHelper.KEY_CONTENT,bookDBHelper.KEY_ISFAV};
Cursor cursor=database.query(bookDBHelper.TABLE_LVS,colums,bookDBHelper.KEY_TYPE + " = \"" +bookDBHelper.TYPE_INDX + "\"",
null,null,null,bookDBHelper.KEY_ID);
if (cursor.moveToFirst()){
do {
season season = new season();
season.setId(cursor.getLong(cursor.getColumnIndex(bookDBHelper.KEY_ID)));
season.setStitle(cursor.getString(cursor.getColumnIndex(bookDBHelper.KEY_CONTENT)));
season.setFav(cursor.getInt(cursor.getColumnIndex(bookDBHelper.KEY_ISFAV)) > 0);
items.add(season);
}while (cursor.moveToNext());
}
return items;
}
public void CopyDbfile()
{
try {
InputStream in=this.context.getAssets().open("lvs.db");
File dir=new File("/data/data/"+this.context.getPackageName()+"/databases");
dir.mkdirs();
FileOutputStream out=new FileOutputStream(new File(dir,"lvs.db"));
byte[] buffer=new byte[1024];
int len=0;
while ((len=in.read(buffer))>0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
Log.i(LVSwitches.LOGTAG,"database copied");
}
catch (Exception e){
Log.i("Story", "copy() : exception -> " + e.getMessage());
}
}
}
season class:
public class season {
private String stitle="";
private String pic="";
private long id=0;
private boolean isFav=false;
public season(String stitle){
this.id= id;
this.stitle= stitle;
this.isFav= isFav;
}
public String getStitle() {
return stitle;
}
public void setStitle(String stitle) {
this.stitle = stitle;
}
public season()
{
String stitle="";
String pic="";
long id=0;
boolean isFav=false;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean isFav() {
return isFav;
}
public void setFav(boolean fav) {
isFav = fav;
}
public String toString(){
return stitle;
}
}
答案1
得分: 0
你需要告诉ListView使用你的数据源。你可以通过以下方式告诉它:
listView.setAdapter(YOUR_ADAPTER);
只需查阅Android文档。编写适配器有详细的说明。
此外,你提到了应用程序在主线程上执行了过多的工作而引发的错误。这与你的ListView问题无关。这更像是一个警告,提示你在另一个线程上执行繁重的任务。主线程用于渲染你的应用程序。因此,如果你在主线程上做太多的工作,你的UI将被阻塞。我建议你在另一个线程上处理数据库操作。此外,你应该记住,只有主线程才能对UI进行更改,因此当你想在UI线程上处理并行线程计算结果时,你可能会发现你的activity对象中的runOnUiThread()
方法很有帮助。
英文:
You need to tell the ListView to use your data source. You can tell it to do so by writing
listView.setAdapter(YOUR_ADAPTER);
Just have a look at the Android documentation. Writing an adapter is well explained.
Furthermore, you mentioned the error that the application does too much work on its main thread. It does not have something to do with your ListView issue. It's much more a warning telling you to do heavy tasks on another Thread. The main thread is used to render your application. So if you do too much work on it your UI is blocked. I'd recommend you to do the database stuff on another thread. Moreover you should keep in mind that only the main thread can apply changes to the UI, so you might find the runOnUiThread() method of your activity object helpful when you want to do something with the result of your parallel thread's calculation on the UI thread.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论