I am a little bit stuck up. First, I’ll post the screenshot and then ask my question.

You can see that some ovals are checked. What I did was maintain 2 sets of images and when the user clicks, the the white image becomes the black.
Now, I need to collect the answers from this answer sheet and want to know how. I tried a method called getImageResource() but that doesn’t exist. Please help me how to go about.
Code:
public class QuizActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
RelativeLayout r2;
// Global variable(s)
int[][] quizData; // Storing the quiz specifications in an integer array
int[][] questionImages = {
{ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
R.drawable.i, R.drawable.j },
{ R.drawable.a_checked, R.drawable.b_checked, R.drawable.c_checked,
R.drawable.d_checked, R.drawable.e_checked,
R.drawable.f_checked, R.drawable.g_checked,
R.drawable.h_checked, R.drawable.i_checked,
R.drawable.j_checked },
{ R.drawable.zero, R.drawable.one, R.drawable.two,
R.drawable.three, R.drawable.four, R.drawable.five,
R.drawable.six, R.drawable.seven, R.drawable.eight,
R.drawable.nine, R.drawable.decimal },
{ R.drawable.zero_checked, R.drawable.one_checked,
R.drawable.two_checked, R.drawable.three_checked,
R.drawable.four_checked, R.drawable.five_checked,
R.drawable.six_checked, R.drawable.seven_checked,
R.drawable.eight_checked, R.drawable.nine_checked,
R.drawable.decimal_checked } };
// End
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Beginning of variable declarations
ScrollView s1 = new ScrollView(this);
RelativeLayout r1 = new RelativeLayout(this);
r2 = r1;
File quizSpecs = new File("mnt/sdcard/teacher_1.csv"); // Read the file
BufferedReader csvReader = null;
String line = ""; // Storing each line in a string
StringTokenizer currentLine = null;
int noOfQuestions = 0; // Number of questions in the quiz
int time = 0; // Duration of the quiz
int i = 0, j = 0, k = 0; // Loop variables
int previd = 0;
// End of variable declarations
try {
csvReader = new BufferedReader(new FileReader(quizSpecs));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line = csvReader.readLine();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentLine = new StringTokenizer(line, ",");
noOfQuestions = Integer.parseInt(currentLine.nextToken());
time = Integer.parseInt(currentLine.nextToken());
// System.out.println(noOfQuestions + " " + time);
while (currentLine.hasMoreTokens()) {
currentLine.nextToken();
}
quizData = new int[noOfQuestions][6];
for (i = 0; i < noOfQuestions; i++) {
try {
line = csvReader.readLine();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentLine = new StringTokenizer(line, ",");
for (j = 0; j < 6; j++) {
quizData[i][j] = Integer.parseInt(currentLine.nextToken());
// System.out.println(quizData[i][j]);
}
}
try {
csvReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (i = 0; i < noOfQuestions; i++) {
TextView questionNo = new TextView(this);
questionNo.setText(String.valueOf(i + 1));
questionNo.setId(1000 * (i + 1));
questionNo.setTextSize(18);
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p1.addRule(RelativeLayout.BELOW, previd);
previd = (1000 * (i + 1));
questionNo.setLayoutParams(p1);
r1.addView(questionNo, p1);
switch (quizData[i][1]) {
case 1:
case 2:
for (j = 0; j < quizData[i][2]; j++) {
ImageView option = new ImageView(this);
option.setImageResource(questionImages[0][j]);
option.setId((1000 * (i + 1)) + j + 1);
option.setOnClickListener(this);
/*
* if (j >= quizData[i][2]) {
* option.setVisibility(View.INVISIBLE);
* option.setEnabled(false); }
*/
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, ((1000 * i) + 1));
params.addRule(RelativeLayout.RIGHT_OF,
((1000 * (i + 1)) + j));
previd = ((1000 * (i + 1)) + j);
option.setLayoutParams(params);
r1.addView(option, params);
}
break;
case 3:
for (j = 0; j < (quizData[i][3] == 0 ? quizData[i][2]
+ quizData[i][3] : quizData[i][2] + quizData[i][3] + 1); j++) {
for (k = 10; k > -1; k--) {
ImageView num = new ImageView(this);
num.setImageResource(questionImages[2][10 - k]);
num.setId((1000 * (i + 1)) + (100 * j) + k + 1);
num.setOnClickListener(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (j == 0) {
params.addRule(RelativeLayout.RIGHT_OF,
(1000 * (i + 1)));
} else {
params.addRule(RelativeLayout.RIGHT_OF,
(1000 * (i + 1)) + (100 * (j - 1)) + k + 1);
}
if (k == 10) {
params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
} else {
params.addRule(RelativeLayout.BELOW,
((1000 * (i + 1)) + (100 * j) + k + 2));
}
num.setLayoutParams(params);
r1.addView(num, params);
}
}
previd = (1000 * (i + 1)) + 1;
break;
case 4:
case 5:
for (j = quizData[i][2] - 1; j > -1; j--) {
for (k = 0; k < quizData[i][3]; k++) {
ImageView match = new ImageView(this);
match.setImageResource(questionImages[0][k]);
match.setId((1000 * (i + 1)) + (100 * j) + k + 1);
match.setOnClickListener(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (k == 0) {
params.addRule(RelativeLayout.RIGHT_OF,
(1000 * (i + 1)));
} else {
params.addRule(RelativeLayout.RIGHT_OF,
(1000 * (i + 1)) + (100 * j) + k);
}
if (j == quizData[i][2] - 1) {
params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
} else {
params.addRule(RelativeLayout.BELOW,
(1000 * (i + 1)) + (100 * (j + 1)) + k + 1);
}
match.setLayoutParams(params);
r1.addView(match, params);
}
}
previd = (1000 * (i + 1)) + 1;
break;
}
}
s1.addView(r1, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
this.setContentView(s1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
// Beginning of variable declarations
int clickedButton = v.getId();
int questionNo = clickedButton / 1000; // Finding the question number
int i = 0; // Loop variable
int rowNo = (clickedButton / 100) % 10;
// System.out.println(questionNo);
// System.out.println(quizData[questionNo - 1][1]);
switch (quizData[questionNo - 1][1]) {
case 1:
for (i = 0; i < quizData[questionNo - 1][2]; i++) {
ImageView option = new ImageView(this);
option.setImageResource(questionImages[0][i]);
option.setId((1000 * questionNo) + i + 1);
option.setOnClickListener(this);
/*
* if (i >= quizData[questionNo - 1][2]) {
* option.setVisibility(View.INVISIBLE);
* option.setEnabled(false); }
*/
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,
((1000 * (questionNo - 1)) + 1));
params.addRule(RelativeLayout.RIGHT_OF,
((1000 * questionNo) + i));
option.setLayoutParams(params);
r2.addView(option, params);
}
ImageView option = new ImageView(this);
option.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
option.setId((1000 * questionNo) + (clickedButton % 10));
option.setOnClickListener(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,
((1000 * (questionNo - 1)) + 1));
params.addRule(RelativeLayout.RIGHT_OF, ((1000 * questionNo)
+ (clickedButton % 10) - 1));
option.setLayoutParams(params);
r2.addView(option, params);
break;
case 2:
ImageView checked = new ImageView(this);
checked.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
checked.setId((1000 * questionNo) + (clickedButton % 10));
checked.setOnClickListener(this);
RelativeLayout.LayoutParams params_checked = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params_checked.addRule(RelativeLayout.BELOW,
((1000 * (questionNo - 1)) + 1));
params_checked.addRule(RelativeLayout.RIGHT_OF,
((1000 * questionNo) + (clickedButton % 10) - 1));
checked.setLayoutParams(params_checked);
r2.addView(checked, params_checked);
break;
case 3:
for (i = 10; i > -1; i--) {
ImageView num = new ImageView(this);
num.setImageResource(questionImages[2][10 - i]);
num.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
num.setOnClickListener(this);
RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (rowNo == 0) {
params_num.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo));
} else {
params_num.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo) + (100 * (rowNo - 1)) + i + 1);
}
if (i == 10) {
params_num.addRule(RelativeLayout.BELOW,
(1000 * (questionNo - 1)) + 1);
} else {
params_num.addRule(RelativeLayout.BELOW,
((1000 * questionNo) + (100 * rowNo) + i + 2));
}
num.setLayoutParams(params_num);
r2.addView(num, params_num);
}
ImageView num = new ImageView(this);
num.setImageResource(questionImages[3][11 - (clickedButton % 100)]);
num.setId((1000 * questionNo) + (100 * rowNo)
+ (clickedButton % 100));
num.setOnClickListener(this);
RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (rowNo == 0) {
params_num
.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo));
} else {
params_num.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
+ (100 * (rowNo - 1)) + (clickedButton % 100));
}
if (((clickedButton % 100) - 1) == 10) {
params_num.addRule(RelativeLayout.BELOW,
(1000 * (questionNo - 1)) + 1);
} else {
params_num.addRule(RelativeLayout.BELOW, ((1000 * questionNo)
+ (100 * rowNo) + (clickedButton % 100) + 1));
}
num.setLayoutParams(params_num);
r2.addView(num, params_num);
break;
case 4:
for (i = 0; i < quizData[questionNo - 1][3]; i++) {
ImageView match = new ImageView(this);
match.setImageResource(questionImages[0][i]);
match.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
match.setOnClickListener(this);
RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i == 0) {
params_match.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo));
} else {
params_match.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo) + (100 * rowNo) + i);
}
if (rowNo == quizData[questionNo - 1][2] - 1) {
params_match.addRule(RelativeLayout.BELOW,
(1000 * (questionNo - 1)) + 1);
} else {
params_match.addRule(RelativeLayout.BELOW,
(1000 * questionNo) + (100 * (rowNo + 1)) + i + 1);
}
match.setLayoutParams(params_match);
r2.addView(match, params_match);
}
ImageView match = new ImageView(this);
match.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
match.setId((1000 * questionNo) + (100 * rowNo)
+ (clickedButton % 10));
match.setOnClickListener(this);
RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (((clickedButton % 10) - 1) == 0) {
params_match.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo));
} else {
params_match.addRule(RelativeLayout.RIGHT_OF,
(1000 * questionNo) + (100 * rowNo)
+ (clickedButton % 10) - 1);
}
if (rowNo == quizData[questionNo - 1][2] - 1) {
params_match.addRule(RelativeLayout.BELOW,
(1000 * (questionNo - 1)) + 1);
} else {
params_match.addRule(RelativeLayout.BELOW,
(1000 * (questionNo)) + (100 * (rowNo + 1))
+ (clickedButton % 10));
}
match.setLayoutParams(params_match);
r2.addView(match, params_match);
break;
case 5:
ImageView match_checked = new ImageView(this);
match_checked
.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
match_checked.setId((1000 * questionNo) + (100 * rowNo)
+ (clickedButton % 10));
match_checked.setOnClickListener(this);
RelativeLayout.LayoutParams params_mc = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (((clickedButton % 10) - 1) == 0) {
params_mc.addRule(RelativeLayout.RIGHT_OF, 1000 * questionNo);
} else {
params_mc.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
+ (100 * rowNo) + (clickedButton % 10) - 1);
}
if (rowNo == quizData[questionNo - 1][2] - 1) {
params_mc.addRule(RelativeLayout.BELOW,
(1000 * (questionNo - 1)) + 1);
} else {
params_mc.addRule(RelativeLayout.BELOW, (1000 * (questionNo))
+ (100 * (rowNo + 1)) + (clickedButton % 10));
}
match_checked.setLayoutParams(params_mc);
r2.addView(match_checked, params_mc);
System.out.println(match_checked.getDrawable());
break;
}
}
}
Instead of tracking the image that is currently being used, you should instead think of the problem in terms of a Data Model. The Data Model holds the state of the application. It looks to me like what you have is a set of questions, each question has different possible answers,and then there are the selected answer/answers. If you model the data out appropriate you can apply whatever kind of View logic you want for the state of the model. This is straight out of an MVC (Model View Controller) paradigm.
I doubt your application as it is now will scale at all and thus you should define some kind of Model, which could easily just be done with a few classes (Question class, Answer class) and you can define the relationships a few different ways. From there you would simply write out the View based on what the current state of the model is.. In android this is done with Adapters and an AdapterView. Typically you wouldn’t use these directly and in your case you’d probably use a ListView with a ListAdapter. What you will do is when the user clicks something you will capture that event, then update the model. Your ListAdapter will listen for changes to the model through a DataSetObserver, however you could just listen for clicks and make the changes directly in the onclick listeners instead of going the full distance of having an observer.
This obviously isn’t something you could likely do in an hour or two unless you were real familiar with all these things but IMO it would make your application much cleaner, easier to debug, and easier to test and more robust.