0

我想创建一个自定义 ExpandableListView 如下。

我正在开发一个简单的问答游戏应用程序。

在游戏结束时,我想在 ExpandableListView 中显示问题及其答案(每个问题都是父级,单击一个问题会打开它的子​​级,即该问题的答案)。

问题和答案必须有不同的颜色(将来可能会有不同的布局和不同的图像等):红色表示错误回答的问题和错误选择的答案,绿色表示正确回答的问题和正确选择的答案,黑色表示错误的答案用户没有选择。

为此,我创建了一个简单的自定义类。

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;
}

然后,我为 ExpandableListAdapter 创建了一个类

public class MyExpandableListAdapter extends BaseExpandableListAdapter { ... }

我从我的应用程序类中调用

    [...]
    private HashMap<MyListItem, ArrayList<MyListItem>> listOfGameAnswers;
    listAdapter = new MyExpandableListAdapter(this, listOfGameAnswers);
    [...]

但是,我无法使 MyExpandableListAdapter 工作(如果需要,我可以提供代码)。

我的方法是正确的还是必须 MyExpandableListAdapter 输入类似

映射<字符串,其他列表>?

有没有更好的方法来实现它?

PS:我参考了这个讨论更改文本颜色 如何更改可扩展列表中某些标题中的文本颜色

非常感谢

更新:解决方案

我接受了下面的解决方案,因为它很简单并且应该可以很好地工作。

无论如何,我也设法修复了我的代码,如下所示:

@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);

}
4

2 回答 2

2
//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));     
  } 
}

然后在您的适配器中:

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;
    }
}

实现getGroupViewgetChildView方法

于 2013-09-19T12:22:51.313 回答
1

试试这个链接...

你会得到这样的输出 在此处输入图像描述

于 2013-09-19T13:19:07.120 回答