I would like to create a custom ExpandableListView as follows.
I am developing a simple quiz game application.
At the end of the game, I want to show the questions and their answers in an ExpandableListView (each question is a parent, clicking on a question opens its children that are the answers for that question).
Questions and answers have to be in different colors (and in future, possibly different layouts with different images, etc.): RED for wrong answered questions and wrong chosen answers, GREEN for correct answered questions and correct chosen answers, BLACK for wrong answers the user did not choose.
To do that, I created a simple custom class.
public class MyListItem {
// the text of the item, that can either be a question or an answer
public String text;
// 0 = not chosen and wrong (BLACK)
// 1 = correct (GREEN)
// 2 = wrong (RED)
public int textColor = 0;
}
Then, I created a class for the ExpandableListAdapater
public class MyExpandableListAdapter extends BaseExpandableListAdapter { ... }
which I call from my application class
[...]
private HashMap<MyListItem, ArrayList<MyListItem>> listOfGameAnswers;
listAdapter = new MyExpandableListAdapter(this, listOfGameAnswers);
[...]
However, I cannot make the MyExpandableListAdapter work (I can provide code if needed).
Is my approach correct or must the MyExpandableListAdapter have in input something like
Map < String, list of something else > ?
Is there a better method to implement it?
P.S.: I referred to this discussion for changing the text color How to change text color in some headers in Expandable List
Thanks a lot
UPDATE: SOLUTION
I accepted the solution below because it is simple and should work well.
Anyway I managed to fix my code as well, as follows:
@Override
public Object getGroup(int groupPosition) {
ArrayList<MyListItem> myKeyList = new ArrayList<MyListItem>(listOfQuestions.keySet());
return myKeyList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// get KEY
MyListItem t = (MyListItem) getGroup(groupPosition);
//get arraylist of my elements from key (which is an element!)
ArrayList<MyListItem> mylist = this.listOfQuestions.get(t);
// return child
return mylist.get(childPosition);
}
//base class that holds data
public class QuestionWithAnswers {
public String question;
public int rightAnswer = 0;
List<String> answers = new ArrayList(4);
public QuestionWithAnswers(String question, int rightAnswer, List<String> answers) {
this.question = question;
this.rightAnswer = rightAnswer;
this.answers.addAll(answers);
}
public QuestionWithAnwsers(String question, int rightAnswer, String answers) {
this.question = question;
this.rightAnswer = rightAnswer;
this.answers.addAll(Arrays.asList(answers));
}
}
Then in your adapter:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
final Context context;
final List<QuestionWithAnswers> data;
public MyExpandableListAdapter(Context context, List<QuestionWithAnswers> data) {
this.context = context;
this.data = data;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).answers.size();
}
@Override
public QuestionWithAnswers getGroup(int groupPosition) {
return data.get(groupPosition);
}
@Override
public String getChild(int groupPosition, int childPosition) {
return data.get(groupPosition).answers.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//processing views
return SomeView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//processing views
int rightAnswer = data.get(groupPosition).rightAnswer;
if (childPosition == rightAnswer){
textViewWithAnswer.setTextColor(Color.GREEN);
} else {
textViewWithAnswer.setTextColor(Color.RED);
}
return SomeView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
implement getGroupView
and getChildView
methods