Bonjour à tous,
J'ai actuellement deux activités faisant des requêtes HTTP. Sur la deuxième activité, il y a un bouton précédent. J'aimerai pouvoir revenir sur la première activité sans perdre d'informations car à l'heure actuelle je perds toutes les informations récupérées.
Première activité:
public class GetChildrenList extends AppCompatActivity implements View. OnClickListener {
private ArrayList<Child> childrenImeList = new ArrayList< Child>();
private ListView itemsListView;
private TextView tv_signin_success;
String email;
String password;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState);
setContentView ( R. layout. get_children_list);
String infos_user = ( String) getIntent (). getSerializableExtra ( "infos_user" );
tv_signin_success = ( TextView) findViewById ( R. id. tv_signin_success);
tv_signin_success. setText ( "Bonjour " + infos_user + "!" );
itemsListView = ( ListView) findViewById ( R. id. list_view_children);
new GetChildrenAsync (). execute ();
}
class GetChildrenAsync extends AsyncTask< String, Void, ArrayList< Child>> {
private Dialog loadingDialog;
@Override
protected void onPreExecute () {
super . onPreExecute ();
loadingDialog = ProgressDialog. show ( GetChildrenList. this , "Please wait" , "Loading..." );
}
@Override
protected ArrayList<Child> doInBackground ( String... params) {
InputStream is = null ;
int statusCode = 0 ;
int id = 0 ;
int age = 0 ;
email = ( String) getIntent (). getSerializableExtra ( "email" );
password = ( String) getIntent (). getSerializableExtra ( "password" );
String result = null ;
String first_name = null ;
String last_name = null ;
try {
HttpClient httpClient = new DefaultHttpClient ();
HttpGet httpGet = new HttpGet ();
URI website = new URI ( "http://192.168.1.33:5000/childrenime/list" );
httpGet. setURI ( website);
String base64EncodedCredentials = Base64. encodeToString (
( email + ":" + password). getBytes (),
Base64. NO_WRAP);
httpGet. setHeader ( "Authorization" , "Basic " + base64EncodedCredentials);
HttpResponse response = httpClient. execute ( httpGet);
statusCode = response. getStatusLine (). getStatusCode ();
HttpEntity entity = response. getEntity ();
is = entity. getContent ();
BufferedReader reader = new BufferedReader ( new InputStreamReader ( is, "UTF-8" ), 8 );
StringBuilder sb = new StringBuilder ();
String line = null ;
while (( line = reader. readLine ()) != null ) {
sb. append ( line + " \n " );
}
result = sb. toString ();
String jsonResult = "{ \" children \" :" + result + "}" ;
Log. d ( "result1" , jsonResult);
JSONObject jsonObject = new JSONObject ( jsonResult);
JSONArray childrenArray = jsonObject. getJSONArray ( "children" );
for ( int i = 0 ; i < childrenArray. length (); ++ i) {
JSONObject child = childrenArray. getJSONObject ( i);
id = child. getInt ( "id" );
first_name = child. getString ( "first_name" );
last_name = child. getString ( "last_name" );
age = child. getInt ( "age" );
String name = first_name + " " + last_name;
childrenImeList. add ( new Child ( id, name, age));
}
} catch ( ClientProtocolException e) {
e. printStackTrace ();
} catch ( UnsupportedEncodingException e) {
e. printStackTrace ();
} catch ( IOException e) {
e. printStackTrace ();
} catch ( JSONException e) {
e. printStackTrace ();
} catch ( URISyntaxException e) {
e. printStackTrace ();
}
return childrenImeList;
}
@Override
protected void onPostExecute ( final ArrayList<Child> childrenListInformation) {
loadingDialog. dismiss ();
CustomListChildrenAdapter adapter = new CustomListChildrenAdapter ( GetChildrenList. this , childrenListInformation);
itemsListView. setAdapter ( adapter);
}
}
}
Deuxième activité:
public class GetLearningGoalsList extends AppCompatActivity implements View. OnClickListener {
private ArrayList<LearningGoal> childrenLearningList = new ArrayList< LearningGoal>();
private Button btn_previous;
private ListView itemsListView;
String email;
String password;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState);
setContentView ( R. layout. get_learning_goals_list);
btn_previous = ( Button) findViewById ( R. id. btn_previous);
btn_previous. setOnClickListener ( this );
itemsListView = ( ListView) findViewById ( R. id. list_view_learning_goals);
new GetLearningGoalsAsync (). execute ();
}
@Override
public void onClick ( View v) {
Intent myIntent = new Intent ( GetLearningGoalsList. this , GetChildrenList. class );
myIntent. setFlags ( Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity ( myIntent);
finish ();
return ;
}
class GetLearningGoalsAsync extends AsyncTask< String, Void, ArrayList< LearningGoal>> {
private Dialog loadingDialog;
@Override
protected void onPreExecute () {
super . onPreExecute ();
loadingDialog = ProgressDialog. show ( GetLearningGoalsList. this , "Please wait" , "Loading..." );
}
@Override
protected ArrayList<LearningGoal> doInBackground ( String... params) {
InputStream is = null ;
int statusCode = 0 ;
int id = 0 ;
email = ( String) getIntent (). getSerializableExtra ( "email" );
password = ( String) getIntent (). getSerializableExtra ( "password" );
int idChild = ( int ) getIntent (). getSerializableExtra ( "idChild" );
String json = null ;
String result = null ;
String name = null ;
String start_date = null ;
String end_date = null ;
try {
HttpClient httpClient = new DefaultHttpClient ();
HttpPost httpPost = new HttpPost ();
URI website = new URI ( "http://192.168.1.33:5000/learningchild/list" );
httpPost. setURI ( website);
JSONObject jsonObj = new JSONObject ();
jsonObj. accumulate ( "idChild" , idChild);
json = jsonObj. toString ();
StringEntity se = new StringEntity ( json);
httpPost. setEntity ( se);
String base64EncodedCredentials = Base64. encodeToString (
( email + ":" + password). getBytes (),
Base64. NO_WRAP);
httpPost. setHeader ( "Authorization" , "Basic " + base64EncodedCredentials);
httpPost. setHeader ( "Accept" , "application/json" );
httpPost. setHeader ( "Content-type" , "application/json" );
HttpResponse response = httpClient. execute ( httpPost);
statusCode = response. getStatusLine (). getStatusCode ();
HttpEntity entity = response. getEntity ();
is = entity. getContent ();
BufferedReader reader = new BufferedReader ( new InputStreamReader ( is, "UTF-8" ), 8 );
StringBuilder sb = new StringBuilder ();
String line = null ;
while (( line = reader. readLine ()) != null ) {
sb. append ( line + " \n " );
}
result = sb. toString ();
String jsonResult = "{ \" learningGoals \" :" + result + "}" ;
Log. d ( "result1" , jsonResult);
JSONObject jsonObject = new JSONObject ( jsonResult);
JSONArray learningGoalsArray = jsonObject. getJSONArray ( "learningGoals" );
for ( int i = 0 ; i < learningGoalsArray. length (); ++ i) {
JSONObject learningGoal = learningGoalsArray. getJSONObject ( i);
id = learningGoal. getInt ( "id" );
name = learningGoal. getString ( "name" );
start_date = learningGoal. getString ( "start_date" );
end_date = learningGoal. getString ( "end_date" );
childrenLearningList. add ( new LearningGoal ( id, name, start_date, end_date));
}
} catch ( ClientProtocolException e) {
e. printStackTrace ();
} catch ( UnsupportedEncodingException e) {
e. printStackTrace ();
} catch ( IOException e) {
e. printStackTrace ();
} catch ( JSONException e) {
e. printStackTrace ();
} catch ( URISyntaxException e) {
e. printStackTrace ();
}
return childrenLearningList;
}
@Override
protected void onPostExecute ( final ArrayList<LearningGoal> learningListInformation) {
loadingDialog. dismiss ();
CustomListLearningGoalAdapter adapter = new CustomListLearningGoalAdapter ( GetLearningGoalsList. this , learningListInformation);
itemsListView. setAdapter ( adapter);
}
}
}
Comment pourrai-je faire pour ne pas perdre d'informations?
Merci à vous.
Afficher la suite
30 mai 2017 à 09:21
Merci à toi.
30 mai 2017 à 09:34
Perso, j'ai une préférence pour la 2nde solution. A quoi te sert tes Activities?
30 mai 2017 à 09:36
30 mai 2017 à 10:08
Modifié le 5 juin 2017 à 14:50
Je viens de me remettre sur ce problème là. J'ai suivi tes indications seulement vu que j'ai une classe extends BaseAdapter, j'ai un peu du mal à les mettre en place...