Reputation: 2828
Ok, so I just recently got into this Android app development using Eclipse for fun over this winter break, so forgive me if this is a very easy question.
I have so far an app where it asks a question (I create the questions in the program) and the user types the answer and hits submit, pretty simple. My problem is, how do I get the app to go onto the next question? Here is what i have so far:
package first.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import java.util.Random;
public class FirstProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Champion ahri = new Champion(0, "Ahri", "I am a Mage", false);
Champion akali = new Champion(1, "Akali", "Ninja stuff", true);
Champion alistar = new Champion(2, "Alistar", "I am a cow", false);
final LinkedList champions = new LinkedList();
champions.insert(ahri);
champions.insert(akali);
champions.insert(alistar);
question = (TextView) findViewById(R.id.question);
txtname = (EditText) findViewById(R.id.txt_name);
blah = (Button) findViewById(R.id.btn_clickme);
quit = (Button) findViewById(R.id.quit);
quit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
Champion tmp;
//Random randomGenerator = new Random();
//int randomNumber = randomGenerator.nextInt(2);
//tmp = champions.find(randomNumber);
/*while (tmp.hasBeenCalled) {
randomNumber = randomGenerator.nextInt(2);
tmp = champions.find(randomNumber);
}*/
/*tmp.hasBeenCalled = true;
while (tmp.name == null) {
Toast.makeText(getBaseContext(), "You have completed the quiz!", Toast.LENGTH_SHORT);
blah.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You have completed the quiz!", Toast.LENGTH_SHORT);
}
});
}*/
tmp = champions.getFirst();
// while (tmp.name != null); {
question.setText(tmp.says);
final Champion temp = tmp;
blah.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 = txtname.getText().toString();
if (champions.isCorrect(temp.name, str1)) {
Toast.makeText(getBaseContext(), getString(R.string.correct), Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getBaseContext(), getString(R.string.incorrect), Toast.LENGTH_SHORT).show();
}
});
//tmp.hasBeenCalled = true;
tmp = champions.getNext();
//question.setText(tmp.says);
// }
}
private EditText txtname;
private Button blah;
private Button quit;
private TextView question;
}
Ok, so I have a Link
and LinkedList
and Champions
.java file and all that. My problem is, when I try to loop (the while loop that is commented out) it just breaks the app, nothing shows up when I simulate it. But I don't know how else to get the next Question onto the page. Do I have to create a screen for each question? Do I have to write each question out over and over? Any help is appreciated. Sorry if i am not very clear. (and the code, I really am a pac rat even when I code, I dont like to delete code until I for sure know I won't need it).
Upvotes: 3
Views: 1272
Reputation: 33509
Your while
loops are most likely blocking the UI thread. This will cause your app to not respond and hang/force close/crash.
You will want to refactor your code to not use blocking while
loops.
Try simply displaying the question then acting on the event of a user giving the answer (I don't know if that will involve clicking a button or entering text or numbers in a box). If the answer is correct, get the next question and display it.
You want to make your application more event driven, don't wait around looping for an answer.
I think this article may have some helpful tidbits: http://developer.android.com/guide/practices/design/responsiveness.html
Upvotes: 1