0

i want to make a questionnaire with Android Studio. Each question can be answered with yes or no. The problem is, that i have some follow-up questions (f.e. if question 1 answered with yes, then i have 3 follow-up questions to answer). Maybe it is important to know, that there are only follow-up questions if the user is clicking on the yes Button for some questions (there are never follow-up questions if he is clicking no)

I am an absolute beginner, so tell me if you have other ideas to solve this problem.

My idea is to make a custom listview and if the user is clicking on the first answer, i want to update the list and add questions if they are needed now (would be ok if the new questions are getting to the end of the list).

That's my Code till now:

questions_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/questionsListView"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:descendantFocusability="blocksDescendants"/>
</RelativeLayout>

itemView.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/question"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<RadioGroup
    android:orientation="horizontal"
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/question">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yes"
        android:id="@+id/yesButton"
        android:layout_marginLeft="135dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:id="@+id/noButton"
        android:layout_marginRight="50dp"/>

</RadioGroup>


</RelativeLayout>

LoggedInActivity.java:

public class LoggedInActivity extends Activity {
public List<Questions> myquestions = new ArrayList<Questions>();
public List<Integer> answerList = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.questions_activity);

    populateQuestions();
    populateListView();
    registerClickCallback();


}

private void registerClickCallback(){
    ListView list =  (ListView) findViewById(R.id.questionsListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
            boolean clickedQuestion = myquestions.get(position).getAnswer();

        }
    });
}

private void populateQuestions() {
    myquestions.add(new Questions("Question1"));
    myquestions.add(new Questions("Question2"));
    myquestions.add(new Questions("Question3"));


}

private void populateListView() {
    ArrayAdapter<Questions> adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.questionsListView);
    list.setAdapter(adapter);
}



private class MyListAdapter extends ArrayAdapter<Questions> {
    public MyListAdapter() {
        super(LoggedInActivity.this, R.layout.item_view, myquestions);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
        }

        Questions currentQuestion = myquestions.get(position);

        TextView question = (TextView) itemView.findViewById(R.id.question);
        question.setText(currentQuestion.getQuestion());

        RadioGroup answers = (RadioGroup)         itemView.findViewById(R.id.radioGroup);

        return itemView;
    }
}
  1. The OnClickListener doesn't work at all. I know, i need to do something with the RadioGroup in getView, but i don't know how (already tried to progress with solutions from here(Customized ListView with TextView, TextView, RadioGroup in Android) but it doesn't worked.

  2. Is it even possible to add new questions and delete them if he is changing his opinion or are there better solutions?

Community
  • 1
  • 1
SLIZER
  • 1
  • 1
  • The book [Android Programming: The Big Nerd Ranch Guide](https://www.bignerdranch.com/we-write/android-programming/) has a questionnaire example. – Alexander Farber Sep 20 '15 at 20:49

1 Answers1

0

I would offer you solutions on the onClick Listener part

Try any of the following steps

1. Try making use of your onClick listener in the onCreate method directly

2 You can implement View.OnClickListener, after that you create an onclick method. Inside the onclick method you can now perform any action you wish to perform

james jelo4kul
  • 839
  • 4
  • 17