这是我的代码:
private void InitialContact(final Context context)
{
final ContactDatabase db = new ContactDatabase(context);
context.openOrCreateDatabase("****", MODE_PRIVATE, null);
if(refrech){
refrech(db, context);
}
getcontact(db, context);
db.close();
}
getContact fonction(在主线程中)
private void getcontact(final ContactDatabase db, final Context context){
SQLiteDatabase dbread = db.getReadableDatabase();
String[] retour = {
"pseudo",
"adresse",
"image", "ID"
};
Cursor cursor = dbread.query("Contact", retour, null, null, null, null, "pseudo");
while(cursor.moveToNext()) {
String pseudo = cursor.getString(cursor.getColumnIndexOrThrow("pseudo"));
Log.d("debug", "getcontact: "+pseudo);
listcontact.add(new Contact(pseudo));
}
cursor.close();
}
Asynchrone任务刷新
private void refrech(final ContactDatabase db, final Context context){
final SQLiteDatabase dbwrite = db.getWritableDatabase();
db.onRefresh(dbwrite);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
int ID = preferences.getInt("ID", 0);
if(ID != 0){
ApiInterface mApiService = this.getInterfaceService();
Call<ContactCall> mService = mApiService.getcontact(ID);
mService.enqueue(new Callback<ContactCall>() {
@Override
public void onResponse(Call<ContactCall> call, Response<ContactCall> response) {
ContactCall ContactCallobjet = response.body();
ArrayList pseudo = ContactCallobjet.isLogin;
for(int i = 0 ;pseudo.size() <= i; i++){
ContentValues values = new ContentValues();
String contact = (String) pseudo.get(i);
values.put("pseudo", contact);
values.put("adresse", "test");
values.put("image", "test");
values.put("ID", "5");
dbwrite.insert("Contact", null, values);
}
}
@Override
public void onFailure(Call<ContactCall> call, Throwable t) {
Log.d("DEBUG", "populateWithInitialContacts: "+t.getMessage());
}
});
} else {
Log.d("DEBUG", "populateWithInitialContacts: Error ID");
}
}
我的问题是在asynchrone函数(refrech)之前调用getContact函数 我测试了一个CallBack但它没有用
答案 0 :(得分:0)
在onResponse和失败回调中调用getContact
方法:
private void InitialContact(final Context context) {
final ContactDatabase db = new ContactDatabase(context );
context.openOrCreateDatabase("****", MODE_PRIVATE, null);
if(refrech){
refrech(db, context);
} else {
getcontact(db, context);
}
db.close();
}
private void refrech(final ContactDatabase db, final Context context){
final SQLiteDatabase dbwrite = db.getWritableDatabase();
db.onRefresh(dbwrite);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
int ID = preferences.getInt("ID", 0);
if(ID != 0){
ApiInterface mApiService = this.getInterfaceService();
Call<ContactCall> mService = mApiService.getcontact(ID);
mService.enqueue(new Callback<ContactCall>() {
@Override
public void onResponse(Call<ContactCall> call, Response<ContactCall> response) {
ContactCall ContactCallobjet = response.body();
ArrayList pseudo = ContactCallobjet.isLogin;
for(int i = 0 ;pseudo.size() <= i; i++){
ContentValues values = new ContentValues();
String contact = (String) pseudo.get(i);
values.put("pseudo", contact);
values.put("adresse", "test");
values.put("image", "test");
values.put("ID", "5");
dbwrite.insert("Contact", null, values);
// call get contacts after the response
getcontact(db, context);
}
}
@Override
public void onFailure(Call<ContactCall> call, Throwable t) {
Log.d("DEBUG", "populateWithInitialContacts: "+t.getMessage());
// call get contacts on error
getcontact(db, context);
}
});
} else {
Log.d("DEBUG", "populateWithInitialContacts: Error ID");
// call get contacts on error id
getcontact(db, context);
}
}