Ho aggiornato il codice:
public class Step1Training extends Activity {
ImageFragment myImageFragment;
InputFragment myInputFragment;
Drawable cross;
ImageView myImageView;
EditText myEditText;
Button continueButton;
int length;
int i = 0;
String rating;
Handler handler;
boolean continueTask;
int[] screenshots = {
R.drawable.googleplaybooks2,
R.drawable.aldiko1,
R.drawable.chaton2,
R.drawable.cinetrailer1,
R.drawable.fanpage1
};
String filePATH;
Intent intent;
File root;
boolean fileDeleted;
TextHandler textHandler;
int participantID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.step1_training);
fileDeleted = false;
root = Environment.getExternalStorageDirectory();
textHandler = new TextHandler();
intent = getIntent();
filePATH = intent.getStringExtra("file_path");
participantID = intent.getIntExtra("participant_ID", participantID);
System.out.println(filePATH);
System.out.println(participantID);
handler = new Handler();
length = screenshots.length;
initUI();
startThread();
addListenerOnContinueButton();
}
private void initUI(){
myImageFragment = (ImageFragment)getFragmentManager().findFragmentById(R.id.imageFragment);
myInputFragment = (InputFragment)getFragmentManager().findFragmentById(R.id.inputFragment);
myImageView = (ImageView)myImageFragment.getView().findViewById(R.id.screenshotImageView);
myImageView.setImageResource(R.drawable.cross300x300);
continueButton = (Button) myInputFragment.getView().findViewById(R.id.continueButton);
continueButton.setVisibility(View.GONE);
myEditText = (EditText) myInputFragment.getView().findViewById(R.id.ratingEditText);
myEditText.setText("");
myEditText.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View arg0, int arg1, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(arg1 == KeyEvent.KEYCODE_ENTER) )
{
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
myEditText.setVisibility(View.GONE);
myEditText.setOnKeyListener(null);
}
private void startThread(){
handler.postDelayed(new TaskScreenshot(0), 300);
handler.postDelayed(new TaskNoise(), 800);
handler.postDelayed(new TaskEditText(), 1050);
myEditText.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_ENTER)) {
if(myEditText.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Inserire un numero da 1 a 9", Toast.LENGTH_SHORT).show();
return false;
} else if (Integer.parseInt(myEditText.getText().toString()) >= 1 &&
Integer.parseInt(myEditText.getText().toString()) <= 9){
// System.out.println(i);
startThread2(i+1);
return true;
}
}
return false;
}
});
}
private void startThread2(final int i){
if(i < length){
handler.postDelayed(new TaskCross(), 300);
handler.postDelayed(new TaskScreenshot(i), 600);
handler.postDelayed(new TaskNoise(), 1100);
handler.postDelayed(new TaskEditText(), 1350);
myEditText.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_ENTER)) {
if(myEditText.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Inserire un numero da 1 a 9", Toast.LENGTH_SHORT).show();
return false;
} else if (Integer.parseInt(myEditText.getText().toString()) >= 1 &&
Integer.parseInt(myEditText.getText().toString()) <= 9){
System.out.println(i);
startThread2(i+1);
if(i == length-1 && myEditText.getVisibility() == View.VISIBLE){
continueButton.setVisibility(View.VISIBLE);
}
return true;
}
}
return false;
}
});
}
}
private void addListenerOnContinueButton() {
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Step1Training.this, MainSession1.class);
intent.putExtra("file_path", filePATH);
intent.putExtra("participand_ID", participantID);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Attenzione")
.setMessage("Sei sicuro/a di voler uscire dall'applicazione?\n" +
"Confermando uscirai dall'applicazione e perderai \n" +
"tutti i dati raccolti finora.")
.setCancelable(false)
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setPositiveButton("Si", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
fileDeleted = textHandler.deleteFile(filePATH);
if(fileDeleted){
System.out.println("CANCELLATO!!");
} else {
System.out.println("NON CANCELLATO!!");
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
class TaskScreenshot implements Runnable{
int j;
public TaskScreenshot(int _i){
this.j = _i;
}
@Override
public void run() {
System.out.println("Screenshot");
myEditText.setText("");
myEditText.setVisibility(View.GONE);
myImageFragment.getView().setVisibility(View.VISIBLE);
myImageView.setImageResource(screenshots[j]);
myImageView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
myImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
}
}
class TaskNoise implements Runnable{
@Override
public void run() {
System.out.println("Noise");
myImageView.setImageResource(R.drawable.noise);
myImageView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
myImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
}
}
class TaskEditText implements Runnable{
@Override
public void run() {
System.out.println("EditText");
myEditText.setVisibility(View.VISIBLE);
myImageFragment.getView().setVisibility(View.GONE);
}
}
class TaskCross implements Runnable{
@Override
public void run() {
System.out.println("Cross");
myEditText.setVisibility(View.GONE);
myImageFragment.getView().setVisibility(View.VISIBLE);
myImageView.getLayoutParams().width = 60;
myImageView.getLayoutParams().height = 60;
myImageView.setImageResource(R.drawable.cross300x300);
}
}
}
Questo codice funziona parzialmente perchè avendo 5 immagini, vengono visualizzate la 1°, la 3° e la 5°. Perchè? C'è qualcosa di sbagliato con l'indice "i"?
Se qualcuno può aiutarmi, è da un pò di giorni che provo risolvere questo problema..
Grazie!