I'm very new to programming as well as Android deveopment so please pardon me if my basics aren't well versed yet. I have provided my code at the bottom.
As mentioned in the title, how can I reference each view among the views created by my custom arrayadapter? So that I could access variables of each view object in the arrayadapter.
Actually, I have 2 questions to what I need to achieve. So right now I am trying to create a simple app which consist of a science test. I have managed to display all the questions(TextViews) paired with a CheckBox view into a ListView layout. My idea is that when a user checks the CheckBox, it will store a 'true' Boolean value somewhere (probably in an ArrayList). Afterwards at the score summary activity, I will compare the user's answers with the original Boolean values (the question's correct answer) to check if the right answer was given for each question and return the score. Is it advisable to do this?
TestActivity.java
package com.example.android.scienceknowledgetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// ArrayList of all the questions and their boolean (answer) values
ArrayList<Question> questions = new ArrayList<Question>();
questions.add(new Question("Plants derive most of their dry mass from the air", true, ""));
questions.add(new Question("Aluminium is the most common metal in the Earth's crust", true, ""));
questions.add(new Question("Vitamin C has be shown to prevent colds", false, ""));
questions.add(new Question("We lose most of our heat through our heads", false, ""));
questions.add(new Question("Dogs are unable to digest chocolate", false, ""));
questions.add(new Question("Apple pips contain cyanide", true, ""));
questions.add(new Question("Cholesterol is a naturally-occurring toxic substance", false, ""));
questions.add(new Question("When you're on a diet, you lose weight by oxidising fat to a gas and exhaling it", true, ""));
questions.add(new Question("Human beings are unable to sense when the oxygen level of the air is low", true, ""));
questions.add(new Question("Most of the Y chromosome is passed unchanged from father to son", true, ""));
// Constructing a new TestAdapter instance
TestAdapter adapter = new TestAdapter(this, questions);
// Find the ListView to display the questions and check boxes
ListView questions_checkbox_list = (ListView) findViewById(R.id.question_checkbox_list);
// Display the questions and check boxes to the ListView questions_checkbox_list
questions_checkbox_list.setAdapter(adapter);
// Configuring the Button Submit
Button submitAnswers = (Button) findViewById(R.id.submit_answers);
submitAnswers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent endTest = new Intent(TestActivity.this, TestSummary.class);
startActivity(endTest);
}
});
}
}
TestAdapter
package com.example.android.scienceknowledgetest;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.ArrayList;
public class TestAdapter extends ArrayAdapter<Question> {
// TestAdapter constructor
public TestAdapter(Activity context, ArrayList<Question> questions) {
super(context, 0, questions);
}
private View listItemsView;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the current view is recycled, otherwise inflate new view
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Find the current Question object and store in the variable currentQuestion
Question currentQuestion = getItem(position);
// Find the TextView texts of the Question and set the question
TextView questionText = (TextView) listItemsView.findViewById(R.id.question_text);
questionText.setText(currentQuestion.getmQuestion());
// Find the CheckBox of the Question and set the boolean value
CheckBox answerBool = (CheckBox) listItemsView.findViewById(R.id.answer_boolean);
// Return the item as a view
return listItemsView;
}
}
Question.java
package com.example.android.scienceknowledgetest;
public class Question {
// Declaring the question variable
private String mQuestion;
// Declaring the answer variable
private boolean mAnswer;
// Declaring the attempt variable
private String mAttempt;
// Getter method to request for the question
public String getmQuestion() {
return mQuestion;
}
// Getter method to request for the boolean value of the answer
public boolean getmAnswer() {
return mAnswer;
}
// Question Constructor
public Question(String question, boolean answer, String attempt) {
mQuestion = question;
mAnswer = answer;
mAttempt = attempt;
}
}
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<ListView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="@+id/question_checkbox_list" />
<Button
android:layout_weight="0"
android:id="@+id/submit_answers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit_answers"
android:textSize="16sp"
android:layout_margin="16dp"
android:layout_marginLeft="48dp"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<CheckBox
android:id="@+id/answer_boolean"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
tools:text="When you're on a diet, you lose weight by oxidising fat to a gas and exhaling it"/>
</LinearLayout>