Search code examples
java-memidp

how to add record to into array


I got a problem with RecordStore when I fetch it and put it into array I got multiple results.

I mean when I have 3 records and fetch it by enumerateRecords then put it into array[][] and then fetch it again, I know that is wrong way to fetch again, but how can I add resultset of recordstore into array[][] and then fetch resultset of array[][]?

My Code:

 public Table showRs(){
    String sID = null,sName = null,sTOE = null;
    hrs.openRecordStore(hrs.getRecordnameBasic());
    //hrs.listofRecord();
    RecordEnumeration re;
    try {
        re = hrs.getRcs().enumerateRecords(null, null, true);
        while(re.hasNextElement()){
             byte[] recordBuffer = re.nextRecord();
             String record = new String(recordBuffer);

             //ID
             int iID = record.indexOf(";");
              sID = record.substring(0,iID);
             //Name
             int iName = record.indexOf(";", iID+1);
              sName = record.substring(iID + 1,iName);
             //Type of Est
             int iTOE = record.indexOf(";", iName + 1);
              sTOE = record.substring(iName + 1, iTOE);

              int rc = hrs.getRcs().getNumRecords();
                tb = new Table(mf, this, "List of record");
                TableCell cells[][] = new TableCell[rc][3];

                for(int i = 0; i< rc ; i++){
                System.out.println("-------" + sID);
                    cells[i][0] = new TableCell(CellType.STRING, sID, true);
                    cells[i][1] = new TableCell(CellType.STRING, sName, true);
                    cells[i][2] = new TableCell(CellType.STRING, sTOE, true);
                }
                String header[] = new String[]{"ID", "Name", "TypeOfEst"};
                tb.setData(new int[]{40,97,98}, header, cells);




        }


    } catch (Exception e) {
        e.printStackTrace();
    }

    return tb;
}

Table

import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class Table extends Canvas implements CommandListener {

    private MIDlet midlet;
    private Displayable preScreen;
    private String tableName;
    private int oneRowHeight;
    private int Countrow = 2;
    private int Countcol = 2;
    protected int focusRow = 0;// cursor focus on row
    protected int focusCol = 0;// cursor focus on col
    private int pad = 0;// pad between row
    private int scrollBarWidth = 4;// SCroll bar for table
    private int headerHeight;
    private String header[] = {" ", " "};//Table header
    private int colWidth[] = {118, 118};// total width screen
    public TableCell cells[][] = {{new TableCell(CellType.STRING, "Name", false), new TableCell(CellType.STRING, "Bush", true)}, {new TableCell(CellType.STRING, "Sex", false), new TableCell(CellType.ITEM, "Male", new String[]{"Male", "Female"}, true)}};
    private Font font = Font.getDefaultFont();// table font
    private Command editCommand;
    private Command backCommand;
    private CellEditor cellEditor;//cell can be edit

    public Table(MIDlet midlet, Displayable preScreen, String tableName) {
        this.midlet = midlet;
        this.preScreen = preScreen;
        this.tableName = tableName;
        init();
        repaint();
    }

    private void init() {
        setTitle(tableName);
        editCommand = new Command("Edit", Command.ITEM, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        addCommand(editCommand);
        addCommand(backCommand);
        setCommandListener(this);
        calculateTableHeight();
        repaint();
    }

   /*
     *This method allow me set data into table
     *and set total width for table and table header
     */
    public void setData(int colWidth[], String header[], TableCell[][] cells) {
        if (colWidth.length != cells[0].length || (header != null && colWidth.length != header.length)) {
            System.out.println("Invalid Argument.");
            return;
        }
        this.colWidth = colWidth;
        this.cells = cells;
        this.header = header;
        Countrow = cells.length;
        Countcol = cells[0].length;
        calculateTableHeight();
        repaint();
    }
    /*
     * Set table's font
     */
    public void setFont(Font font) {
        this.font = font;
        calculateTableHeight();
        repaint();
    }

    /*
     *This method calculate all cells' height.
     *Long cell text will be splited into several segments(several lines).
     */
    protected void calculateTableHeight() {
        oneRowHeight = font.getHeight() + 1;//depends on height of font
        if(header==null){
            headerHeight=0;
        }else{
            headerHeight = oneRowHeight;
        }

        int xTemp = 0;
        int yTemp = headerHeight;
        int rowHeight=oneRowHeight;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < Countrow; i++) {
            rowHeight = oneRowHeight;
            xTemp = 0;

            for (int j = 0; j < Countcol; j++) {
                cells[i][j].x = xTemp;
                xTemp += colWidth[j];
                cells[i][j].y = yTemp;
                cells[i][j].width = colWidth[j];

                if (cells[i][j].cellText == null || font.stringWidth(cells[i][j].cellText) < colWidth[j]) {
                    cells[i][j].height = oneRowHeight;
                    cells[i][j].multiLine = false;
                } else {
                    cells[i][j].multiLine = true;
                    cells[i][j].cellString = new Vector();// create vector to store String in that cell
                    sb.setLength(0);
                    for (int k = 0; k < cells[i][j].cellText.length(); k++) {
                        sb.append(cells[i][j].cellText.charAt(k));//append string into sb until the end of string
                        if (font.stringWidth(sb.toString()) > colWidth[j]) {
                            sb.deleteCharAt(sb.length() - 1);
                            cells[i][j].cellString.addElement(sb.toString());
                            sb.setLength(0);
                            sb.append(cells[i][j].cellText.charAt(k));
                        }
                    }
                    if (sb.length() > 0) {
                        cells[i][j].cellString.addElement(sb.toString());
                    }
                    cells[i][j].height = oneRowHeight * cells[i][j].cellString.size();
                }

                if (rowHeight < cells[i][j].height) {
                    rowHeight= cells[i][j].height;
                }
            }
            for (int j = 0; j < Countcol; j++) {
                cells[i][j].height = rowHeight;
            }
            yTemp += cells[i][0].height;
        }
    }

    protected void paint(Graphics g) {
        g.setFont(font);
        //draw table background
        g.setColor(0xffffff);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());

        //draw table border line
        g.setColor(0x000000);
        g.drawLine(0, 0, cells[0][Countcol-1].x+cells[0][Countcol-1].width, 0);
        g.drawLine(0, 0, 0, cells[Countrow-1][0].y+cells[Countrow-1][0].height);
        g.drawLine(cells[0][Countcol-1].x+cells[0][Countcol-1].width, 0, cells[0][Countcol-1].x+cells[0][Countcol-1].width, cells[Countrow-1][0].y+cells[Countrow-1][0].height);
        g.drawLine(0, cells[Countrow-1][0].y+cells[Countrow-1][0].height, cells[0][Countcol-1].x+cells[0][Countcol-1].width, cells[Countrow-1][0].y+cells[Countrow-1][0].height);

        //draw cells
        for (int i = 0; i < Countrow; i++) {
            //draw cell background
            if (i % 2 == 0) {
                g.setColor(0xe7f3f8);
                g.fillRect(1, cells[i][0].y - pad + 1, cells[i][Countcol - 1].x + cells[i][Countcol - 1].width, cells[i][0].height - 2);
            }
            g.setColor(0x000000);
            g.drawLine(0, cells[i][0].y - pad + cells[i][0].height, cells[i][Countcol - 1].x + cells[i][Countcol - 1].width, cells[i][0].y + cells[i][0].height - pad);

            //draw cell text
            for (int j = 0; j < Countcol; j++) {
                //draw single-line text
                if (!cells[i][j].multiLine) {
                    if (cells[i][j].cellText != null) {
                        g.drawString(cells[i][j].cellText, cells[i][j].x + 1, cells[i][j].y - pad + 1, 0);
                    }
                } else {
                    //draw multi-line text
                    for (int a = 0; a < cells[i][j].cellString.size(); a++) {
                        g.drawString(cells[i][j].cellString.elementAt(a).toString(), cells[i][j].x + 1, cells[i][j].y + oneRowHeight * a - pad + 1, 0);
                    }
                }
            }
        }
        //draw table header
        if (header != null) {
            g.setColor(0xA0A0A0);
            g.fillRect(1, 1, cells[0][Countcol - 1].x + cells[0][Countcol - 1].width, headerHeight);
            g.setColor(0x000000);
            g.drawLine(0, 0, cells[0][Countcol - 1].x + cells[0][Countcol - 1].width, 0);
            g.drawLine(0, headerHeight, cells[0][Countcol - 1].x + cells[0][Countcol - 1].width, headerHeight);
            for (int i = 0; i < header.length; i++) {
                g.drawString(header[i], cells[0][i].x + 1, 0, 0);
            }
        }

        //draw vertical line
        int temp = 0;
        for (int i = 0; i < colWidth.length; i++) {
            temp += colWidth[i];
            g.drawLine(temp, 0, temp, cells[Countrow - 1][0].y + cells[Countrow - 1][0].height);
        }

        //draw scrollbar
        g.drawLine(this.getWidth() - scrollBarWidth, 0, this.getWidth() - scrollBarWidth, this.getHeight() - 1);
        g.fillRect(this.getWidth() - scrollBarWidth, 0, scrollBarWidth, ((int) (this.getHeight() * (focusRow + 1) / Countrow)));

        //draw focus cell
        g.setColor(0x3a9ff7);
        g.fillRect(cells[focusRow][focusCol].x + 1, cells[focusRow][focusCol].y - pad + 1, cells[focusRow][focusCol].width - 1, cells[focusRow][focusCol].height - 1);
        g.setColor(0x000000);
        if (!cells[focusRow][focusCol].multiLine) {
            if (cells[focusRow][focusCol].cellText != null) {
                g.drawString(cells[focusRow][focusCol].cellText, cells[focusRow][focusCol].x + 1, cells[focusRow][focusCol].y - pad + 1, 0);
            }
        } else {
            for (int i = 0; i < cells[focusRow][focusCol].cellString.size(); i++) {
                g.drawString(cells[focusRow][focusCol].cellString.elementAt(i).toString(), cells[focusRow][focusCol].x + 1, cells[focusRow][focusCol].y + oneRowHeight * i - pad + 1, 0);
            }
        }
    }

    public void commandAction(Command com, Displayable display) {
        if (com == backCommand) {
            Display.getDisplay(midlet).setCurrent(preScreen);
        } else if (com == editCommand) {
            if (cells[focusRow][focusCol].editable) {
                editCell(cells[focusRow][focusCol]);
            }
        }
    }

    private void editCell(TableCell cell) {
        if (cellEditor == null) {
            cellEditor = new CellEditor(midlet, this, "");
        }
        cellEditor.setCell(cell);
        Display.getDisplay(midlet).setCurrent(cellEditor);
    }

    public void keyPressed(int keyCode) {
        switch (keyCode) {
            case -3: //left

                focusCol--;
                if (focusCol <= 0) {
                    focusCol = 0;
                }
                break;

            case -4: //right

                focusCol++;
                if (focusCol >= Countcol - 1) {
                    focusCol = Countcol - 1;
                }

                break;

            case -1:  //up

                focusRow--;
                if (focusRow <= 0) {
                    focusRow = 0;
                }
                if (cells[focusRow][0].y < pad+headerHeight) {
                    pad = cells[focusRow][0].y-headerHeight;
                }

                break;

            case -2: //down

                focusRow++;
                if (focusRow >= Countrow - 1) {
                    focusRow = Countrow - 1;
                }

                if (cells[focusRow][0].y + cells[focusRow][0].height-pad> this.getHeight()) {
                    pad = cells[focusRow][0].y + cells[focusRow][0].height - this.getHeight();
                }

                break;
            case 13:
                if (cells[focusRow][focusCol].editable) {
                    editCell(cells[focusRow][focusCol]);
                }
                break;
        }
        repaint();
    }
}

Table Cell

import java.util.Vector;


public class TableCell {

    int x, y, width, height;

    int cellType;// types of cell - String , int or other type.

    boolean editable = false;// cell can be edit or not.

    boolean multiLine = false;// long text can be split into several lines.

    public Vector cellString;

    public String[] itemOptions;// itemOptions used when cell type is Item.

    public String cellText;

    public TableCell(){
        this(CellType.STRING, "", true);
    }


     public TableCell(int cellType, String cellText, boolean editable) {
        this.cellType = cellType;
        this.cellText = cellText;
        this.editable = editable;
    }

    public TableCell(int cellType, String cellText, String[] itemOptions, boolean editable){
        this.cellType = cellType;
        this.cellText = cellText;
        this.itemOptions = itemOptions;
        this.editable = editable;
    }

    /*
     *all objects need to be represent by string
     *
     */
    public String toString(){
        return "TableCell: x=" + x + ",y=" + y + ",width=" + width + ",height=" + height+",cellText="+cellText;
    }


}

Cell Type

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

package ui.table;

public class CellType {

        public static final int STRING = 0;
        public static final int NUMBER = 1;
        public static final int ITEM = 2;

}

Cell Editor

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;


public class CellEditor extends  Form implements CommandListener {

     private MIDlet midlet = null;
    private Table previousScreen = null;
    private Command backCommand = null;
    private Command OKCommand = null;
    private TextField textField;
    private ChoiceGroup choiceGroup;
    private TableCell cell;

    public CellEditor(MIDlet midlet, Table previousScreen, String title) {
        super(title);
        this.midlet = midlet;
        this.previousScreen = previousScreen;
        initialize();
    }

    private void initialize() {
        backCommand = new Command("Back", Command.BACK, 1);
        OKCommand = new Command("OK", Command.OK, 1);
        addCommand(backCommand);
        addCommand(OKCommand);
        setCommandListener(this);
    }

    public void setCell(TableCell cell) {
        this.cell = cell;
        deleteAll();
        if (cell.cellType == CellType.STRING) {
            textField = getTextField();
            textField.setConstraints(TextField.ANY);
            textField.setString(cell.cellText);
            append(textField);
        } else if (cell.cellType == CellType.NUMBER) {
            textField = getTextField();
            textField.setConstraints(TextField.NUMERIC);
            textField.setString(cell.cellText);
            append(textField);
        } else {
            choiceGroup=getChoiceGroup();
            choiceGroup.deleteAll();
            for (int i = 0; i < cell.itemOptions.length; i++) {
                choiceGroup.append(cell.itemOptions[i], null);
                if (cell.cellText.equals(cell.itemOptions[i])) {
                    choiceGroup.setSelectedIndex(i, true);
                }
            }
            append(choiceGroup);
        }
    }

    public TextField getTextField() {
        if (textField == null) {
            textField = new TextField("", "", 300, TextField.ANY);
        }
        return textField;
    }

    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {
            choiceGroup = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
        }
        return choiceGroup;
    }

    public void commandAction(Command com, Displayable display) {

        if (com == backCommand) {
            Display.getDisplay(midlet).setCurrent(previousScreen);
        } else if (com == OKCommand) {
            if (cell.cellType == CellType.ITEM) {
                previousScreen.cells[previousScreen.focusRow][previousScreen.focusCol].cellText = choiceGroup.getString(choiceGroup.getSelectedIndex());
            } else {
                previousScreen.cells[previousScreen.focusRow][previousScreen.focusCol].cellText = textField.getString();
            }
            previousScreen.calculateTableHeight();
            Display.getDisplay(midlet).setCurrent(previousScreen);
        }
    }

}

And I want to output data from ResultSet


Solution

  • It's still not quite clear what exactly is the problem, but it feels like you better try dropping internal for loop and moving table / cells declaration outside of while:

     //...
        String sID = null, sName = null, sTOE = null;
        hrs.openRecordStore(hrs.getRecordnameBasic());
        //hrs.listofRecord();
        RecordEnumeration re;
        try {
            // move table / cells declaration outside of while:
            int rc = hrs.getRcs().getNumRecords();
            tb = new Table(mf, this, "List of record");
            TableCell cells[][] = new TableCell[rc][3];
            int i = 0;
    
            re = hrs.getRcs().enumerateRecords(null, null, true);
            while(re.hasNextElement()){
                 byte[] recordBuffer = re.nextRecord();
                 //... stuff that was there
                 sTOE = record.substring(iName + 1, iTOE);
    
                 // drop that loop - for(int i = 0; i< rc ; i++){
                 System.out.println("-------" + sID);
                 cells[i][0] = new TableCell(CellType.STRING, sID, true);
                 cells[i][1] = new TableCell(CellType.STRING, sName, true);
                 cells[i][2] = new TableCell(CellType.STRING, sTOE, true);
                 i++;
            }
            String header[] = new String[]{"ID", "Name", "TypeOfEst"};
            tb.setData(new int[]{40,97,98}, header, cells);
        } catch (Exception e) {
            e.printStackTrace();
        }
    //...