
Consider an application that gives a quiz to the user. Are the quiz questions a part of
Button b = new Button(this); b.setTextColor(Color.RED);Button b = findViewById("@+id/button1");Button b = findViewById(R.id.button1);Context. Activity is a subclass of Context.intent.putExtra(keyString, obj);
onCreate of new intent:
getIntent().getXXXExtra(keyString)
getXXXExtra methods.ListView into the activity XMLAdapter to populate listOnItemClickListener to get notified of item selectionvoid onItemClick(AdapterView<?> parent, View view, int position, long id)new ArrayAdapter<String>(activity, itemLayoutId, strings)TextViewgetView(int, View, ViewGroup) in subclasspublic View getView(int position, View v, ViewGroup parent) {
if (v == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemLayoutId, parent);
}
Item item = items.get(position);
((TextView) v.findViewById(R.id.description)).setText(item.getDescription());
((TextView) v.findViewById(R.id.price)).setText(item.getPrice());
return v;
}


private Question question to MainActivity. Change the code to construct a new Question(title, choice1, choice2, choice3, choice4), and set the question instance variable. (The correct choice should start with a *.) Read the text and button labels from the Question object.putExtra to add the question and the index (0, 1, 2, 3) of the current button to the intent. The latter is a bit tricky. Use this outline:
for (int i = 0; i < buttonIds.length; i++) {
final int choiceIndex = i;
((Button) findViewById(buttonIds[i])).setOnClickListener(
new View.OnClickListener() {
@Override public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, ResponseActivity.class);
intent.putExtra("choiceIndex", choiceIndex);
...
}});
}
final, try
intent.putExtra("choiceIndex", choiceIndex) instead. What happens?
getIntExtra and getSerializableExtra. Then call
((TextView) findViewById(R.id.responseText)).setText(
question.isCorrect(choiceIndex) ? "Good job!" : "Try again!");
ArrayAdapter out of the question choices. What is your code?OnItemClickListener. Move the appropriate code from your button listener. What is your code?