When I touch the screen, the OnTouch method is called twice. So the Intent of the new activity in the method "showdetails" load the activity with the view associated twice also. Why? I just need one...
public class DeptFragment extends Fragment implements OnTouchListener {
Context mContext = getActivity();
ImageView cacheImage,targetImage;
ListView List;
ArrayList<String> tabDepartement;
ArrayList<String> tabCommunes;
int codeDepartementCrt;
Departement DepartementCrt;
List<Commune> listCommuneCrt;
Bundle nomBundle;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState) ;
return inflater.inflate(R.layout.dept_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
mContext=getActivity();
View mView = getView();
targetImage = (ImageView) mView.findViewById(R.id.targetDeptImage);
targetImage.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
View mView = getView();
ImageView imageView = (ImageView) mView.findViewById(R.id.cacheDeptImage);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel((int) event.getX(),(int) event.getY());
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
showDetails(redValue,greenValue,blueValue);
return true;
}
/**
* Helper function to show the details of a selected item, by starting a
* whole new activity in which it is displayed.
*/
void showDetails(int red, int green, int blue) {
DepartementCrt=rechercherDepartement(red,green,blue);
if (DepartementCrt != null) {
Intent intent = new Intent();
intent.setClass(getActivity(), DeptDetailActivity.class);
intent.putExtra("code", DepartementCrt.Code);
startActivity(intent);
}
}
private Departement rechercherDepartement(int redValue, int greenValue, int blueValue) {
DepartementRepository departementRepository=new DepartementRepository(mContext);
Departement departement;
departementRepository.Open();
departement= departementRepository.rechercherDepartement(redValue, greenValue, blueValue);
departementRepository.Close();
return departement;
}
}