samedi 26 avril 2014

N'arrive pas à placer tout en boucle à la bonne place - noob Java Android - Stack Overflow


First off, apologies, I'm new to all this and I'm struggling with Android / Java. Ive spent days looking stuff up and I KNOW I'm doing something stupid but would appreciate someone to tell me why / where I'm going wrong.


The app is a simple quiz, puts a picture up, use a spinner to select the answer, press a button to submit, get a toast message to verify and add a score. I know the code is horrendous and a hack but I lack the knowledge to do it more elegantly (part of the reason I'm here). I am trying to place a while loop, using the variable sflags that will be counting up from 0 to 26. When I place it, I can never seem to place it correctly so it works. I suspect some of the code gets broken when I try and wrap it.


Here's the (terrible) code :


public class MainActivity extends Activity implements TextWatcher {
private static final String TAG = "MainActivity";

private EditText mName;
private EditText mEmail;
private ListView countrieslist;
private String comments;
private int score = 0;
private int sflags = 0;
private String emailok;
private String answer;
private String flags;
private Spinner spinnerct;
private Object countries;

// private AdapterView<ListAdapter> spinnerct;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countries = getResources().getStringArray(R.array.flagnames);
mName = (EditText) findViewById(R.id.name);
mEmail = (EditText) findViewById(R.id.email);

mEmail.addTextChangedListener(this);
}

@Override
public void afterTextChanged(Editable s) {
emailok = s.toString();
String sub_but = getString(R.string.sub_but);

boolean valid = emailok.length() > 0 &&
emailok.toLowerCase().indexOf(sub_but) == -1;

View view = findViewById(R.id.imageButton1);


boolean isVisible = view.getVisibility() == View.VISIBLE;
if (isVisible == valid) {
// No animation required if both values are true or both values are
// false
return;
}
Animation anim;

if (valid) {
view.setVisibility(View.VISIBLE);
// Create a new animation object
anim = AnimationUtils.makeInAnimation(this, true);
} else {
// Create a new animation object
anim = AnimationUtils.makeOutAnimation(this, true);
view.setVisibility(View.INVISIBLE);
}
// Tell the view it's time to start animating
view.startAnimation(anim);
}

public void thequiz(View view) {

setContentView(R.layout.activity_quiz);

Toast.makeText(
this.getApplicationContext(),
"Thanks ! Now try to identify the flags of these European Countries!",
Toast.LENGTH_LONG
).show();

sflags = 0;
score = 0;

// LinearLayOut Setup
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

// ImageView Setup
ImageView imageView = new ImageView(this);

// Constructing the filename using "flag" + the item number from
// variable loop
// using GetIndentifier to return resource to a string

// while loop start here?
//while (sflags<27){

// display correct flag
imageView.setImageResource(
this.getResources().getIdentifier("drawable/flag" + sflags, null, this.getPackageName())
);
// setting image position
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
);
// =================================================================
// creating spinner object
final Spinner spinnerct = new Spinner(this);
// Now to populate spinner with contents of array flagnames[]
ArrayAdapter<String> spinnercountry = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.flagnames)
);
spinnerct.setAdapter(spinnercountry);
// creating button
Button myButton = new Button(this);
myButton.setText("Submit Answer");
myButton.setBackgroundColor(Color.rgb(250, 200, 250));
// ===================================================================
// adding view to layout
linearLayout.addView(imageView);
linearLayout.addView(spinnerct);
linearLayout.addView(myButton);
// Show layout
setContentView(linearLayout);

// OnclickListener to see when button is clicked
//=========================================================
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
answer = spinnerct.getSelectedItem().toString();
Toast.makeText(
MainActivity.this,
"You selected " + answer,
Toast.LENGTH_SHORT
).show();
// now check the answer is right by calling checkanswer()
//=======================================================
boolean tester = false;
checkanswer(tester); // jumps to procedure, returns boolean

if (tester = true) {
score = score + 1;
Toast.makeText(
MainActivity.this,
"Well done, correct answer! Your Score is " + score + " out of 27",
Toast.LENGTH_LONG
).show();
}
//========================================================
if (tester != true) {
Toast.makeText(
MainActivity.this,
"Sorry wrong answer! ",
Toast.LENGTH_SHORT
).show();
}
//=========================================================
sflags = sflags + 1;
}
});
};


// sendSMS();
// sendEmail();
// }

//=======================================================
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}
//========================================================
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}
//=========================================================
public void checkanswer(boolean Isitright) {
if (sflags == 0 && answer == "Estonia") {
Isitright = true;
}

}
//=======================================================
}


First off, apologies, I'm new to all this and I'm struggling with Android / Java. Ive spent days looking stuff up and I KNOW I'm doing something stupid but would appreciate someone to tell me why / where I'm going wrong.


The app is a simple quiz, puts a picture up, use a spinner to select the answer, press a button to submit, get a toast message to verify and add a score. I know the code is horrendous and a hack but I lack the knowledge to do it more elegantly (part of the reason I'm here). I am trying to place a while loop, using the variable sflags that will be counting up from 0 to 26. When I place it, I can never seem to place it correctly so it works. I suspect some of the code gets broken when I try and wrap it.


Here's the (terrible) code :


public class MainActivity extends Activity implements TextWatcher {
private static final String TAG = "MainActivity";

private EditText mName;
private EditText mEmail;
private ListView countrieslist;
private String comments;
private int score = 0;
private int sflags = 0;
private String emailok;
private String answer;
private String flags;
private Spinner spinnerct;
private Object countries;

// private AdapterView<ListAdapter> spinnerct;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countries = getResources().getStringArray(R.array.flagnames);
mName = (EditText) findViewById(R.id.name);
mEmail = (EditText) findViewById(R.id.email);

mEmail.addTextChangedListener(this);
}

@Override
public void afterTextChanged(Editable s) {
emailok = s.toString();
String sub_but = getString(R.string.sub_but);

boolean valid = emailok.length() > 0 &&
emailok.toLowerCase().indexOf(sub_but) == -1;

View view = findViewById(R.id.imageButton1);


boolean isVisible = view.getVisibility() == View.VISIBLE;
if (isVisible == valid) {
// No animation required if both values are true or both values are
// false
return;
}
Animation anim;

if (valid) {
view.setVisibility(View.VISIBLE);
// Create a new animation object
anim = AnimationUtils.makeInAnimation(this, true);
} else {
// Create a new animation object
anim = AnimationUtils.makeOutAnimation(this, true);
view.setVisibility(View.INVISIBLE);
}
// Tell the view it's time to start animating
view.startAnimation(anim);
}

public void thequiz(View view) {

setContentView(R.layout.activity_quiz);

Toast.makeText(
this.getApplicationContext(),
"Thanks ! Now try to identify the flags of these European Countries!",
Toast.LENGTH_LONG
).show();

sflags = 0;
score = 0;

// LinearLayOut Setup
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

// ImageView Setup
ImageView imageView = new ImageView(this);

// Constructing the filename using "flag" + the item number from
// variable loop
// using GetIndentifier to return resource to a string

// while loop start here?
//while (sflags<27){

// display correct flag
imageView.setImageResource(
this.getResources().getIdentifier("drawable/flag" + sflags, null, this.getPackageName())
);
// setting image position
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
);
// =================================================================
// creating spinner object
final Spinner spinnerct = new Spinner(this);
// Now to populate spinner with contents of array flagnames[]
ArrayAdapter<String> spinnercountry = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.flagnames)
);
spinnerct.setAdapter(spinnercountry);
// creating button
Button myButton = new Button(this);
myButton.setText("Submit Answer");
myButton.setBackgroundColor(Color.rgb(250, 200, 250));
// ===================================================================
// adding view to layout
linearLayout.addView(imageView);
linearLayout.addView(spinnerct);
linearLayout.addView(myButton);
// Show layout
setContentView(linearLayout);

// OnclickListener to see when button is clicked
//=========================================================
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
answer = spinnerct.getSelectedItem().toString();
Toast.makeText(
MainActivity.this,
"You selected " + answer,
Toast.LENGTH_SHORT
).show();
// now check the answer is right by calling checkanswer()
//=======================================================
boolean tester = false;
checkanswer(tester); // jumps to procedure, returns boolean

if (tester = true) {
score = score + 1;
Toast.makeText(
MainActivity.this,
"Well done, correct answer! Your Score is " + score + " out of 27",
Toast.LENGTH_LONG
).show();
}
//========================================================
if (tester != true) {
Toast.makeText(
MainActivity.this,
"Sorry wrong answer! ",
Toast.LENGTH_SHORT
).show();
}
//=========================================================
sflags = sflags + 1;
}
});
};


// sendSMS();
// sendEmail();
// }

//=======================================================
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}
//========================================================
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}
//=========================================================
public void checkanswer(boolean Isitright) {
if (sflags == 0 && answer == "Estonia") {
Isitright = true;
}

}
//=======================================================
}

0 commentaires:

Enregistrer un commentaire