如何解决android跳过了44帧!该应用程序可能在其主线程上做过多的工作
我正在使用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();
//dataSource.getAllSeasons();
if (seasons.size()==0){
this.dataSource.CopyDbfile();
this.seasons = this.dataSource.getAllSeasons();
}
}
}
这是数据源的代码
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 + "\"",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,len);
}
in.close();
out.close();
Log.i(LVSwitches.LOGTAG,"database copied");
}
catch (Exception e){
Log.i("Story","copy() : exception -> " + e.getMessage());
}
}
}
季节班:
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;
}
}
解决方法
您需要告诉ListView使用您的数据源。您可以通过编写命令告诉它这样做
listView.setAdapter(YOUR_ADAPTER);
只需看一下Android文档。编写适配器已经很好地解释了。
此外,您提到了该应用程序在其主线程上执行过多工作的错误。它与您的ListView问题无关。这更是一条警告,告诉您在另一个线程上执行繁重的任务。主线程用于呈现您的应用程序。因此,如果您对此进行过多工作,则您的UI会被阻止。我建议您在另一个线程上执行数据库工作。此外,您应该记住,只有主线程才能将更改应用于UI,因此当您想要对UI线程上的并行线程的计算结果进行某些操作时,您可能会发现活动对象的runOnUiThread()方法很有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。