List<Map<String, Object>> 遍历

新手上路,请多包涵

要把List<Map<String, Object>>里面
key为"Name", "Phone", "daan"
获取然后放到List<Question> list

Question.java

public class Question {
    public String question;
    public String answerA;
    public String answerB;
    public String answerC;
    public String answerD;
    public int answer;
    public String explaination;
    public int ID;

    public int selectedAnswer;
}

我的实现:

private static List<Map<String, Object>> list_data;

public void init(Context context, String eXCEL_PATH) {
        list_data = FileUtils.readExcel(context, eXCEL_PATH);
    }

public List<Question> getQuestions() {
        List<Question> list = new ArrayList<Question>();

        for (int i = 0; i < list_data.size(); i++) {
            Question question = new Question();
            // HashMap map = (HashMap) list_data.get(i);
            Map map = list_data.get(i);

            Set set = map.keySet();
            for (String key:set) {
                question.question = (String) map.get("Name");
                String name = null;
                name = (String) map.get("Phone");
                String[] tmp = name.split(";");
                question.answerA = tmp[0];
                question.answerB = tmp[1];
                question.answerC = tmp[2];
                question.answerD = tmp[3];
                question.explaination = (String) map.get("daan");
            }
            question.selectedAnswer = -1;
            list.add(question);
        }
        return list;
    }

FileUtils.java

    public static List<Map<String ,Object>> readExcel(Context context, String path) {
        List<Map<String ,Object>> data = new ArrayList<>();
        try {
            InputStream is = new FileInputStream(path);
            Workbook book = Workbook.getWorkbook(is);
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows(); // 行
            int Cols = sheet.getColumns(); // 列
            int colsOfName = 0;
            int rolsOfName = 0;
            int colsOfNum = 0;
            int rolsOfNum = 0;
            int colsdaan = 0;
            int rolsdaan = 0;
            for (int i = 0; i < Cols; ++i) {
                for (int j = 0; j < Rows; ++j) {
                    if (sheet.getCell(i, j).getContents().equals("a")) {
                        colsOfName = i;
                        rolsOfName = j;
                        break;
                    }
                }
            }
            for (int i = 0; i < Cols; ++i) {
                for (int j = 0; j < Rows; ++j) {
                    if (sheet.getCell(i, j).getContents().equals("b")) {
                        colsOfNum = i;
                        rolsOfNum = j;
                        break;
                    }
                }
            }
            for (int i = 0; i < Cols; ++i) {
                for (int j = 0; j < Rows; ++j) {
                    if (sheet.getCell(i, j).getContents().equals("c")) {
                        colsdaan = i;
                        rolsdaan = j;
                        break;
                    }
                }
            }

            Map<String,Object> map = new HashMap<>();
            for (int i1 = rolsOfName + 1, i2 = rolsOfNum + 1, i3 = rolsdaan + 1; i1 < (Rows - rolsOfName); i1++, i2++, i3++) {
                if (!sheet.getCell(colsOfName, i1).getContents().equals("") && !sheet.getCell(colsOfNum, i2).getContents().equals("") && !sheet.getCell(colsdaan, i3).getContents().equals("")) {
                    map.put("Name", sheet.getCell(colsOfName, i1).getContents());
                    map.put("Phone", sheet.getCell(colsOfNum, i2).getContents());
                    map.put("daan", sheet.getCell(colsdaan, i3).getContents());
                    /*
                    Question question = new Question();
                    question.question = sheet.getCell(colsOfName, i1).getContents();
                    
                    String name = null;
                    name = sheet.getCell(colsOfNum, i2).getContents();
                    String[] tmp = name.split(";");
                    question.answerA = tmp[0];
                    question.answerB = tmp[1];
                    question.answerC = tmp[2];
                    question.answerD = tmp[3];
                    
                    question.explaination = sheet.getCell(colsdaan, i3).getContents();
                    question.selectedAnswer = -1;
                    list.add(question);
                    */
                    data.add(map);
                }
            }
            book.close();
        } catch (Exception e) {
            Log.i("log", e + "");
        }
        return data;
    }

问题:
开始的时候报arrayindexoutofboundsexception
然后修改代码如上
的到的是却是遍历的最后一个值

参考:
http://www.cnblogs.com/wenxia...

http://bbs.csdn.net/topics/39...

http://www.oschina.net/questi...

阅读 8.1k
4 个回答
新手上路,请多包涵

瞟了一眼你遍历map.keyset是在干嘛?

新手上路,请多包涵

new一个List<Question>
遍历List<Map<String, Object>>,把map一个个的取出来,然后取值

new 一个question
然后把question放进question集合里 

这样不行吗?

新手上路,请多包涵

debug看看取值不就知道了吗。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题