0

在这里,我想根据 excel 中的数据动态地将行(自定义布局)添加到 ArrayList。除了我只加载了 ArrayAdapter 的最后一个值之外,一切都工作得很好。

首先是我的实例变量的声明:

List<Record> model = new ArrayList<Record>();
RecordAdapter adapter = null;

这是我认为是有问题的代码的一部分。

class RecordAdapter extends ArrayAdapter<Record> {
    RecordAdapter() {
        super(Census.this, R.layout.row, model);
    }

    public View getView(int position, View convertView,
                        ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);
            holder = new RecordHolder(row);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }
        holder.populateFrom(model.get(position));
        return (row);
    }
}

这是 RecordHolder 类:

static class RecordHolder {
    TextView tvEntryNo = null;
    TextView tvPNO = null;
    TextView tvDefoliator = null;
    TextView tvLpF = null;
    CheckBox cbOryctes = null;
    CheckBox cbTermite = null;
    CheckBox cbRat = null;
    private ImageView icon = null;

    RecordHolder(View row) {
        tvEntryNo = (TextView) row.findViewById(R.id.tvEntry);
        tvPNO = (TextView) row.findViewById(R.id.tvPNO);
        tvDefoliator = (TextView) row.findViewById(R.id.tvDefoliator);
        tvLpF = (TextView) row.findViewById(R.id.tvLpF);
        cbOryctes = (CheckBox) row.findViewById(R.id.cbOryctes);
        cbTermite = (CheckBox) row.findViewById(R.id.cbTermite);
        cbRat = (CheckBox) row.findViewById(R.id.cbRat);
        icon = (ImageView) row.findViewById(R.id.icon);
    }

    void populateFrom(Record r) {
        tvEntryNo.setText("Pokok ke: " + r.getEntryNo());
        tvPNO.setText("PNO: " + r.getPNO());
        tvDefoliator.setText("Species: " + r.getDefSpec());
        tvLpF.setText("Larva: " + r.getLpF());
        int temp = 0;

        int counter = 0;

        if (r.isOrycDmg()) {
            cbOryctes.setChecked(true);
        } else {
            cbOryctes.setChecked(false);
        }

        if (r.isTermiteDmg()) {
            cbTermite.setChecked(true);
        } else {
            cbTermite.setChecked(false);
        }

        if (r.isRatDmg()) {
            cbRat.setChecked(true);
        } else {
            cbRat.setChecked(false);
        }

        if ((r.getDefSpec() == "Setora Nitens" || r.getDefSpec() == "Setothosea Asigna" ||
                r.getDefSpec() == "Mahasena Corbetti") && Integer.parseInt(r.getLpF().toString()) > 5) {
            counter++;
        }

        if (!r.getLpF().trim().equals("")) {
            temp = Integer.parseInt(r.getLpF());
        }
        if (temp > 10) {
            counter++;
        }
        if (r.isOrycDmg()) {
            counter++;
        }
        if (r.isTermiteDmg()) {
            counter++;
        }
        if (r.isRatDmg()) {
            counter++;
        }

        if (r.getPnD_Time().trim().equals("")) {
            //New Data
            icon.setImageResource(R.drawable.add);
        } else if (counter == 0) {
            //Negligible problem
            icon.setImageResource(R.drawable.green_alert);
        } else if (counter == 1) {
            //need attention, minor problem
            icon.setImageResource(R.drawable.yellow_alert);
        } else {
            //serious problem
            icon.setImageResource(R.drawable.red_alert);
        }
    }
}

这是我用来从 excel 文件加载数据的 loadData() 方法:

public void loadData() {
    //Instantiate holder
    Record r = new Record();
    try {
        //get the excel file
        workbook = new HSSFWorkbook(file);
        sheet = workbook.getSheetAt(0);

        //parse data from excel to holder
        for (int i = FIRST_ROW_TO_GET; i <= (sheet.getLastRowNum() + 1); i++) {
            Row row = sheet.getRow(i);
            //New Data
            if (row == null) {
                r.setFormNo(filename(fileName));
                r.setEntryNo(String.valueOf(i));
                r.setPNO("Data Baru");
                r.setDefSpec("Tidak Ada");
                r.setLpF("");
                r.setOrycDmg(false);
                r.setTermiteDmg(false);
                r.setRatDmg(false);
                r.setSampler("");
                r.setPnD_Date("");
                r.setPnD_Time("");

            } else {
                //Existing Data
                for (int cn = row.getFirstCellNum(); cn <= row.getLastCellNum(); cn++) {
                    Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
                    //Blank cells handler
                    if (c == null) {
                        switch (cn) {
                            case 0:
                                r.setFormNo(filename(fileName));
                                break;
                            case 1:
                                r.setEntryNo(String.valueOf(i));
                                break;
                            case 2:
                                r.setPNO("Data Baru");
                                break;
                            case 3:
                                r.setDefSpec("Tidak Ada");
                                break;
                            case 4:
                                r.setLpF("");
                                break;
                            case 5:
                                r.setOrycDmg(false);
                                break;
                            case 6:
                                r.setTermiteDmg(false);
                                break;
                            case 7:
                                r.setRatDmg(false);
                                break;
                            case 8:
                                r.setSampler("");
                                break;
                            case 9:
                                r.setPnD_Date("");
                                break;
                            case 10:
                                r.setPnD_Time("");
                                break;
                        }

                    } else {
                        //non empty cells handler
                        switch (cn) {
                            case 0:
                                r.setFormNo(c.getStringCellValue());
                                break;
                            case 1:
                                r.setEntryNo(c.getStringCellValue());
                                break;
                            case 2:
                                r.setPNO(c.getStringCellValue());
                                break;
                            case 3:
                                r.setDefSpec(c.getStringCellValue());
                                break;
                            case 4:
                                r.setLpF(c.getStringCellValue());
                                break;
                            case 5:
                                if (Integer.parseInt(c.getStringCellValue()) == 1)
                                    r.setOrycDmg(true);
                                else
                                    r.setOrycDmg(false);
                                break;
                            case 6:
                                if (Integer.parseInt(c.getStringCellValue()) == 1)
                                    r.setTermiteDmg(true);
                                else
                                    r.setTermiteDmg(false);
                                break;
                            case 7:
                                if (Integer.parseInt(c.getStringCellValue()) == 1)
                                    r.setRatDmg(true);
                                else
                                    r.setRatDmg(false);
                                break;
                            case 8:
                                r.setSampler(c.getStringCellValue());
                                break;
                            case 9:
                                r.setPnD_Date(c.getStringCellValue());
                                break;
                            case 10:
                                r.setPnD_Time(c.getStringCellValue());
                                break;
                        }

                    }
                }
            }
            adapter.add(r);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Record.java 只包含变量的声明。应用程序编译得很好,但我遇到了一个问题,它只使用 LoadData() 循环的最后一个值填充 ListView。我怀疑问题来自我的 getView() 方法。有人可以帮助指出我可能在哪里犯错吗?

我很乐意提供所需的其他代码。提前致谢。:)

4

1 回答 1

1

您需要Record在循环中初始化一个新对象。否则它将更新相同的对象并为每一行显示相同的数据。

public void loadData() {
    try {
        //get the excel file
        workbook = new HSSFWorkbook(file);
        sheet = workbook.getSheetAt(0);

        //parse data from excel to holder
        for (int i = FIRST_ROW_TO_GET; i <= (sheet.getLastRowNum() + 1); i++) {
            Record r = new Record(); // move this HERE
            Row row = sheet.getRow(i);

            ...
            ...
于 2013-11-08T05:01:30.497 回答