2

I get trouble when I try to create a check box in canvas.

My checkbox works well but I don't know how to store value of each item, that mean when user check row 1 , and then they move to another row check box still check row 1, and when user check row 1 and 2 and move to another row, check box will check row 1 and 2.

But I can't find out solution for this problem

MYE
  • 983
  • 8
  • 24
  • 41

1 Answers1

3

modify your code to use selectTodelete as boolean array instead of int, about like shown below

    // ...initialization of DataList
    boolean[] selectTodelete = new boolean[2]; // instead of int
    { selectTodelete[0] = selectTodelete[1] = false; } // init array
    Command editCommand, backCommand,selectCmd, unselectCmd,selectAll;
    //...

    protected void paint(Graphics g) {
        //...
        for(int i =0 ; i<countRow; i++ ){
            //draw background
            //...
                     if(selectTodelete[i]){ // was selectTodelete == 1
                            //draw select dot at location for row 'i'
                            //...
                     }
            // remove: you don't need that anymore: if(selectTodelete == 2) {
                            //draw select dot...
            //}

            // draw a checkbox before each item
            // ...
        }
    }

    public void commandAction(Command c, Displayable d) {
        //...
        if(c == selectCmd){
            selectTodelete[selectedItem] = true;
        }
        if(c== unselectCmd){
            selectTodelete[selectedItem] = false;
        }
        if(c == selectAll){
            selectTodelete[0] = selectTodelete[1] = true;
        }
        repaint();
    }
    //...
}

update - answer to question in comments

I want to get RCID fit to checked it mean when row was checked I can get this id and when I use delete command it will delete all rows were checked

For that, you can expose selectTodelete for use outside of its class with getter, or, better yet, with method like below...

    boolean isSelected(int elementNum) {
        return elementNum >= 0 && elementNum < selectTodelete.length
                && selectTodelete[elementNum];
    } // modelled after javax.microedition.lcdui.Choice.isSelected

...information exposed like that can be further used anywhere when you need it to deal with RCID, like eg in method below:

    Vector useSelection(DataList dataList, DataStore[][] ds) {
        Vector result = new Vector();
        int count = ds.length;
        for(int i = 0; i < count; i++ ) {
            if (!dataList.isSelected(i)) {
                continue; // skip non selected
            }
            System.out.println("RCID selected: [" + ds[i][5].cellText + "]");
            result.addElement(ds[i][5]);
        }
        return result;
    }
gnat
  • 6,213
  • 108
  • 53
  • 73
  • sorry brother, can i ask you one more question. in my code i want to get RCID fit to checked it mean when row was checked i can get this id and when i use delete command it will delete all rows were checked. Thank You – MYE Nov 05 '11 at 11:43
  • @MYE I updated the answer to show how to use `selectTodelete` to deal with RCID – gnat Nov 05 '11 at 17:42
  • Thank you very much, but im wondering, how to add it into array, because i need to get id to delete item – MYE Nov 06 '11 at 04:40
  • im wondering about how to convert Vector in J2Me to int array because i need to get int value to delete record in resultset. In j2me it can be vector().toArray but in J2me how to do this? – MYE Nov 11 '11 at 13:12
  • @MYE if you need int value, no need to convert. To get that value at index `i`, just use vector elementAt method: `((Integer)(vector.elementAt(i))).intValue()` – gnat Nov 14 '11 at 06:31