1.activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.test.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="Cloud Survey"
android:textSize="18sp"
android:textColor="#000"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2.layout_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#353535"/>
<LinearLayout
android:id="@+id/ll_questions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"/>
</LinearLayout>
3.layout_question.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_question_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#000"/>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bad"/>
<RadioButton
android:id="@+id/radio_button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Good"/>
<RadioButton
android:id="@+id/radio_button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Better"/>
<RadioButton
android:id="@+id/radio_button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Best"/>
</RadioGroup>
</LinearLayout>
4.GroupItem.java
public class GroupItem {
private String group_id;
private String group_name;
private List<QuestionItem> LIST_QUESTION;
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public String getGroup_name() {
return group_name;
}
public void setGroup_name(String group_name) {
this.group_name = group_name;
}
public List<QuestionItem> getLIST_QUESTION() {
return LIST_QUESTION;
}
public void setLIST_QUESTION(List<QuestionItem> LIST_QUESTION) {
this.LIST_QUESTION = LIST_QUESTION;
}
}
5.QuestionItem.java
/**
* Create by User on 16/May/2017.
*/
public class QuestionItem {
private String question_id;
private String question_name;
private int answer = -1; //-1 means not yet answer
public String getQuestionId() {
return question_id;
}
public void setQuestionId(String question_id) {
this.question_id = question_id;
}
public String getQuestionName() {
return question_name;
}
public void setQuestionName(String question_name) {
this.question_name = question_name;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}
6.QuestionAdapter.java
public class QuestionAdapter extends BaseAdapter{
private Context context;
private List<GroupItem> LIST_GROUP;
public QuestionAdapter(Context context, List<GroupItem> LIST_GROUP){
this.context = context;
this.LIST_GROUP = LIST_GROUP;
}
@Override
public int getCount() {
return LIST_GROUP.size();
}
@Override
public Object getItem(int position) {
return LIST_GROUP.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.layout_group, parent, false);
}
GroupItem groupItem = LIST_GROUP.get(position);
TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
tv_group.setText(groupItem.getGroup_name());
LinearLayout ll_questions = (LinearLayout) convertView.findViewById(R.id.ll_questions);
ll_questions.removeAllViews();
for (int i = 0; i<groupItem.getLIST_QUESTION().size(); i++){
final QuestionItem questionItem = groupItem.getLIST_QUESTION().get(i);
LayoutInflater inflater1 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View question_view = inflater1.inflate(R.layout.layout_question, null);
TextView tv_question_name = (TextView) question_view.findViewById(R.id.tv_question_name);
tv_question_name.setText(questionItem.getQuestionName());
RadioGroup radioGroup = (RadioGroup) question_view.findViewById(R.id.radio_group);
if (questionItem.getAnswer() == -1){
radioGroup.clearCheck();
}else {
((RadioButton)radioGroup.getChildAt(questionItem.getAnswer())).setChecked(true);
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_button1 :
questionItem.setAnswer(0);
break;
case R.id.radio_button2 :
questionItem.setAnswer(1);
break;
case R.id.radio_button3 :
questionItem.setAnswer(2);
break;
case R.id.radio_button4 :
questionItem.setAnswer(3);
break;
}
//notifyDataSetChanged();
}
});
ll_questions.addView(question_view);
}
return convertView;
}
}
7.MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<GroupItem> LIST_GROUP = new ArrayList<>();
for (int i = 0; i<3; i++){
GroupItem groupItem = new GroupItem();
groupItem.setGroup_name("Group " + (i+1));
List<QuestionItem> LIST_QUESTION = new ArrayList<>();
for (int j = 0; j < 4; j++){
QuestionItem questionItem = new QuestionItem();
questionItem.setQuestionName("Question " + (j+1));
LIST_QUESTION.add(questionItem);
}
groupItem.setLIST_QUESTION(LIST_QUESTION);
LIST_GROUP.add(groupItem);
}
ListView listView = (ListView) findViewById(R.id.listview);
QuestionAdapter adapter = new QuestionAdapter(MainActivity.this, LIST_GROUP);
listView.setAdapter(adapter);
}
}
EmoticonEmoticon