I am trying to understand why a click on a listview item from activity1 takes a log time (around 1 second) to open the second activity.
It seems that my code has an implementation or a performance problem ..
- The first activity (Main) displayed a listview.
- The second activity (Detail_annonce) displayed details of a listview item.
Here is extract of code of my first activity (Main) for bind click on listview item :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Annonce currentAnnonce = (Annonce) list.getAdapter().getItem(position);
String titre = currentAnnonce.getTitle();
String cat = currentAnnonce.getCategorie();
String desc = currentAnnonce.getDescription();
String etat = currentAnnonce.getEtat();
String img = currentAnnonce.getImage();
String prix = currentAnnonce.getPrix();
String villeObj = currentAnnonce.getVilleObjet();
String codePostal = currentAnnonce.getCode_postal();
String departement = currentAnnonce.getDepartement();
String avatarUser = currentAnnonce.getImageUser();
String pseudoUser = currentAnnonce.getPseudoUser();
String dateDebut = currentAnnonce.getDate_debut();
String idAnnonce = currentAnnonce.getId();
String emailUser = currentAnnonce.getEmailUser();
String partel = currentAnnonce.getPartel();
String parmail = currentAnnonce.getParmail();
Intent myIntent = new Intent(Main_All_Annonces.this, Detail_annonce.class);
myIntent.putExtra("titre", titre);
myIntent.putExtra("cat", cat);
myIntent.putExtra("desc", desc);
myIntent.putExtra("etat", etat);
myIntent.putExtra("img", img);
myIntent.putExtra("prix", prix);
myIntent.putExtra("villeObj", villeObj);
myIntent.putExtra("codePostal", codePostal);
myIntent.putExtra("departement", departement);
myIntent.putExtra("avatarUser", avatarUser);
myIntent.putExtra("pseudoUser", pseudoUser);
myIntent.putExtra("dateDebut", dateDebut);
myIntent.putExtra("idAnnonce", idAnnonce);
myIntent.putExtra("emailUser", emailUser);
myIntent.putExtra("partel", partel);
myIntent.putExtra("parmail", parmail);
myIntent.putExtra("parentAct", "AllAnnonces");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// get the common element for the transition in this activity
final View image_view = findViewById(R.id.allannonces_image);
//String transitionName = (String) view.getTag(R.id.allannonces_image);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(
Main_All_Annonces.this,
view, // The view which starts the transition
"transitionImg" // The transitionName of the view we’re transitioning to
);
// put more extras in the intent if you want, like the object clicked
myIntent.putExtra("EXTRA_IMAGE_TRANSITION_NAME", "transitionImg");
ActivityCompat.startActivity(Main_All_Annonces.this, myIntent, options.toBundle());
}
else {
// Code to run on older devices
startActivity(myIntent);
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
}
});
Here is the code on my Detail_annonce activity :
public class Detail_annonce extends Activity {
HttpPost httppost;
StringBuffer buffer;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
DisplayImageOptions options;
LinearLayout btn_mail, btn_tel, btn_share;
View bar1, bar2;
String partel, parmail;
TextView nbAnnonceActionBar ;
@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_layout);
Typeface robotolight = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Roboto/Roboto-Light.ttf");
Typeface robotoBold = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Roboto/Roboto-Bold.ttf");
Typeface robotoRegular = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
bar1 = (View) findViewById(R.id.detail_view1);
bar2 = (View) findViewById(R.id.detail_view2);
options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(2000))
.cacheOnDisc(true)
.build();
Intent previous = getIntent();
Bundle extras = previous.getExtras();
final String titre = extras.getString("titre");
String cat = extras.getString("cat");
String desc = extras.getString("desc");
String img = extras.getString("img");
final String prix = extras.getString("prix");
final String villeObj = extras.getString("villeObj");
final String codePostal = extras.getString("codePostal");
String avatarUser = extras.getString("avatarUser");
final String pseudoUser = extras.getString("pseudoUser");
String dateDebut = extras.getString("dateDebut");
final String idAnnonce = extras.getString("idAnnonce");
final String emailUser = extras.getString("emailUser");
partel = extras.getString("partel");
parmail = extras.getString("parmail");
String parentAct = extras.getString("parentAct");
ImageView image_txt = (ImageView) findViewById(R.id.detail_annonce_image);
TextView titre_txt = (TextView) findViewById(R.id.detail_annonce_titre);
TextView prix_txt = (TextView) findViewById(R.id.detail_annonce_prix);
TextView dateDebut_txt = (TextView) findViewById(R.id.detail_annonce_date_debut);
TextView villeAndCodePostal = (TextView) findViewById(R.id.detail_annonce_ville_and_codePos);
ImageLoader imageLoader = ImageLoader.getInstance();
Double latitude = null, longitude = null;
Geocoder geo = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocationName(villeObj, 10);
Address currentFound = addresses.get(0);
if (currentFound.hasLatitude() && currentFound.hasLongitude()) {
latitude = currentFound.getLatitude();
longitude = currentFound.getLongitude();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ViewCompat.setTransitionName(image_txt, getIntent().getStringExtra("EXTRA_IMAGE_TRANSITION_NAME"));
imageLoader.displayImage(img, image_txt);
titre_txt.setText(titre);
titre_txt.setTypeface(robotoRegular);
prix_txt.setText("€" + prix + ",00 EUR");
prix_txt.setTypeface(robotolight);
villeAndCodePostal.setText("Disponible à " +villeObj);
villeAndCodePostal.setTypeface(robotolight);
////////////////
/// DATE
////////////////
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date pDate;
try {
pDate = df.parse(dateDebut);
dateDebut = DateUtils.getDateDifference(pDate);
dateDebut = dateDebut.replace("-", "");
} catch (ParseException e) {
Log.e("DATE PARSING", "Error parsing date..");
}
dateDebut_txt.setText("Posté il y a " + dateDebut + "dans la catégorie " +cat);
dateDebut_txt.setTypeface(robotolight);
if (latitude != null && longitude != null ) {
// Get a handle to the Map Fragment
GoogleMap map = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap();
LatLng ville = new LatLng(latitude, longitude);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ville, 9));
map.addMarker(new MarkerOptions()
.title(villeObj)
.visible(true)
.snippet("La vente se fait à cet adresse.")
.position(ville));
}
}