I created two activities: the first one contains details of a job offer, and the next one is to postulate to this job. after applying for the job, an alertdialog appears to confirm the success of the operation. However, this alertdialog appears in the view of the job details without values ! How can I manage this?? This is activity 1:
private static final String MY_PREFERENCES = "mespreferences";
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_offre);
ToggleButton precedent = (ToggleButton)findViewById(R.id.btn_preced);
precedent.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent preced = new Intent(DetailsOffre.this, Offres.class);
startActivity(preced);
}
});
Button postuler = (Button)findViewById(R.id.postuler);
postuler.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView id_offre = (TextView) findViewById(R.id.tv_ID_Off1);
SharedPreferences settings = getSharedPreferences(MY_PREFERENCES, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("idoffre", id_offre.getText().toString());
editor.commit();
Intent intent_postul = new Intent(DetailsOffre.this, Candidature.class);
startActivity(intent_postul);
}
});
Button enregistrer = (Button)findViewById(R.id.enregistrer);
enregistrer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
TextView id_offre = (TextView) findViewById(R.id.tv_ID_Off1);
String idOf = id_offre.getText().toString();
Intent intent_enregist = new Intent(DetailsOffre.this, EnregistrerOffre.class);
intent_enregist.putExtra("idoffre",idOf );
startActivity(intent_enregist);
}
});
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
txt.setText("Connexion...");
txt.setText(getServerData(URL2));
}
public static final String URL2 = "http://10.0.2.2/mesRequetes/detail_offr.php";
private String getServerData(String returnString) {
InputStream is = null;
String result = null;
Intent intent3 = getIntent();
String id = intent3.getExtras().getString("idoffre");
ArrayList<NameValuePair> postID = new ArrayList<NameValuePair>();
postID.add(new BasicNameValuePair("idoffre", id));
// Envoie de la commande http
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL2);
httppost.setEntity(new UrlEncodedFormEntity(postID));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
JSONObject detail=null;
for(int i=0;i<jArray.length();i++){
detail = jArray.getJSONObject(i);
TextView numoffre = (TextView) findViewById(R.id.tv_ID_Off1);
numoffre.setText(detail.getString("idoffre"));
TextView nom_societe = (TextView) findViewById(R.id.tv_societe1);
nom_societe.setText(detail.getString("first_name"));
TextView poste = (TextView) findViewById(R.id.TV_post1);
poste.setText(detail.getString("poste"));
TextView ville = (TextView) findViewById(R.id.tv_vill);
ville.setText(detail.getString("ville"));
TextView details = (TextView) findViewById(R.id.tv_detail);
details.setText(detail.getString("details"));
TextView d_crea = (TextView) findViewById(R.id.tv_datecrea);
d_crea.setText(format_d(detail.getString("created_at")));
TextView idste = (TextView) findViewById(R.id.TV_idsociete1);
idste.setText(detail.getString("idsoc"));
SharedPreferences settings = getSharedPreferences(MY_PREFERENCES, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("idsoc", idste.getText().toString());
editor.commit();
Log.i("log_tag","Numero de l offre:"+detail.getInt("idoffre")+
"poste proposé:"+detail.getString("poste")+
"ville:"+detail.getString("ville")+
"details:"+detail.getString("details")+
"date de creation:"+detail.getString("created_at")+
"identifiant de la société:"+detail.getString("idsoc")+
"nom de la société:"+detail.getString("first_name")
);
// Résultats de la requête
returnString += "" + jArray.getJSONObject(i);
};
}catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
public static StringBuilder format_d(final String s) {
String aaaa = s.substring(0, 4);
String mm = s.substring(5, 7);
String dd = s.substring(8, 10);
String heure = s.substring (11);
return new StringBuilder(dd)
.append("/")
.append(mm)
.append("/")
.append(aaaa)
.append(" à ")
.append(heure);
}
活动2:
private static final String MY_PREFERENCES = "mespreferences";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_offre);
SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFERENCES, 0);
String userId = sharedPreferences.getString("id", "");
String idoffr = sharedPreferences.getString("idoffre", "");
Intent intent_postul = getIntent();
ArrayList<NameValuePair> postCandidature= new ArrayList<NameValuePair>();
postCandidature.add(new BasicNameValuePair("idoffre", idoffr));
postCandidature.add(new BasicNameValuePair("id", userId));
this.sendData(postCandidature);
}
private void sendData(ArrayList<NameValuePair> postCandidature) {
// TODO Auto-generated method stub
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/mesRequetes/candidature.php");
httppost.setEntity(new UrlEncodedFormEntity(postCandidature));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
AlertDialog.Builder cand = new AlertDialog.Builder(Candidature.this);
cand.setIcon(R.drawable.succes);
cand.setTitle("Succès");
cand.setMessage("Votre candidature a bien été transmise");
cand.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent offr = new Intent (Candidature.this, Offres.class);
startActivity(offr);
}});
cand.show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
}