将数据行添加到多个表时实现 AsyncTask
Posted
技术标签:
【中文标题】将数据行添加到多个表时实现 AsyncTask【英文标题】:Implement AsyncTask when adding rows of data to multiple tables 【发布时间】:2018-05-29 11:16:59 【问题描述】:我创建了一个显示TextView
和ProgressBar
的屏幕。 ProgressBar
表示使用GSON
创建数据库并将1000++ 数据添加到SQLite
。为了实现这一点,我创建了三个不同的文件,LocalDBHelper
(这是我的数据库设置),GSONHandler
(将我的JSON
文件转换为SQLite
)并加载(为我的AsyncTask
)。
我的问题是,我的ProgressBar
是静态的,它没有显示添加数据的进度。我是 android 开发新手,对我的代码一无所知。
MainActivity.java
public class MainActivity extends AppCompatActivity implements Loading.LoadingTaskFinishedListener
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setupdb_delete);
LocalDBHelper mydb = LocalDBHelper.newInstance(this);
//check if db exist, delete db if exist
if (doesDatabaseExist(mydb.getDatabaseName()))
this.deleteDatabase(mydb.getDatabaseName());
ProgressBar progressBar = findViewById(R.id.progressBar);
new Loading(progressBar, this, this).execute("");
@Override
public void onTaskFinished()
finish();
public boolean doesDatabaseExist(String databaseName)
File dbFile = this.getDatabasePath(databaseName);
return dbFile.exists();
Loading.java
public class Loading extends AsyncTask<String, Integer, Integer>
public interface LoadingTaskFinishedListener
void onTaskFinished();
private LocalDBHelper mydb = null;
private final ProgressBar progressBar;
private Context mContext;
private final LoadingTaskFinishedListener finishedListener;
public Loading(ProgressBar progressBar, LoadingTaskFinishedListener finishedListener, Context context)
this.progressBar = progressBar;
this.finishedListener = finishedListener;
this.mydb = LocalDBHelper.newInstance(context);
mContext = context;
@Override
protected Integer doInBackground(String... params)
SQLiteDatabase sqLiteDatabase = mydb.getWritableDatabase();
GSONHandler.newInstance(mContext, sqLiteDatabase);
return 1234;
@Override
protected void onProgressUpdate(Integer... values)
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
@Override
protected void onPostExecute(Integer result)
super.onPostExecute(result);
finishedListener.onTaskFinished();
GSONHandler.java
public class GSONHandler
static final String TAG = GSONHandler.class.getSimpleName();
private Context context;
private LocalDBHelper mydb;
private SQLiteDatabase sqLiteDatabase;
private static GSONHandler mGsonHandler = null;
public static GSONHandler newInstance(Context context, SQLiteDatabase sqLiteDatabase)
if (mGsonHandler == null)
mGsonHandler = new GSONHandler(context.getApplicationContext(), sqLiteDatabase);
return mGsonHandler;
private GSONHandler(Context context, SQLiteDatabase sqLiteDatabase)
this.context = context;
this.sqLiteDatabase = sqLiteDatabase;
onCreate();
//assign json data to tables
private void onCreate()
//get local json and save it to db
premiseCategoryMasterSetup();
premiseCategorySetup();
inspcTypeSetup();
inspcStatusSetup();
stateSetup();
areaSetup();
districtSetup();
analysisGroupSetup();
analysisSubGroup();
parameterSetup();
subParameterSetup();
identificationSetup();
premiseCertliSetup();
txn_premiseSetup();
prosecutionOtherSetup();
txn_layoutSectionSetup();
txn_layoutCardSetup();
txn_layoutInputFieldSetup();
private void areaSetup()
Log.d(TAG, "areaSetup");
InputStream inputStream = context.getResources().openRawResource(R.raw.ref_area);
String jsonString = readJsonFile(inputStream);
Gson gson = new Gson();
List<Area> areaList = Arrays.asList(gson.fromJson(jsonString, Area[].class));
for (Area area : areaList)
if(sqLiteDatabase != null)
ContentValues values = new ContentValues();
values.put(Constants.COLUMN_AREA_ID, area.getAreaId());
values.put(Constants.COLUMN_AREA_CODE, area.getAreaCode());
values.put(Constants.COLUMN_AREA_NAME, area.getAreaName());
values.put(Constants.COLUMN_ACTIVE, area.getActive());
values.put(Constants.COLUMN_FK_STATE_ID, area.getFk_stateId());
long id = sqLiteDatabase.insertOrThrow(Constants.REF_AREA_TABLE,
null, values);
....
private String readJsonFile(InputStream inputStream)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte bufferByte[] = new byte[1024];
int length;
try
while ((length = inputStream.read(bufferByte)) != -1)
outputStream.write(bufferByte, 0 , length);
outputStream.close();
inputStream.close();
catch (IOException e)
e.printStackTrace();
return outputStream.toString();
【问题讨论】:
使用进度对话框而不是 ProgressBar :) 我认为进度对话框已被弃用?我们被建议使用进度条代替?如果我错了,请纠正我@SubhashPrajapati 【参考方案1】:您正在onProgressUpdate
方法中设置进度条的进度,但它从未被调用。要调用onProgressUpdate
方法,您需要在doInBackground
方法内部调用publishProgress
方法。在调用publishProgress
方法之后,在UI 线程上调用onProgressUpdate
方法。
【讨论】:
您能分享一个代码示例吗?我很困惑..当我尝试添加它时,说ArrayIndexOutOfBoundsException: length=0; index=0
。表中所有数据的添加进度是如何得到的?【参考方案2】:
在doInBackground()
中调用publishProgress();
方法来显示和更新进度条。希望这会有所帮助。
【讨论】:
我尝试在后台添加publishPorgress,但是**onProgressUpdate()**
中的value[0]
正在抛出ava.lang.ArrayIndexOutOfBoundsException: length=0; index=0
,progressBar仍然没有移动。保持100%以上是关于将数据行添加到多个表时实现 AsyncTask的主要内容,如果未能解决你的问题,请参考以下文章
在 clickhouse 中创建表时如何将自定义默认值添加到 Nullable 类型?