1

リストフィールドチェックボックスから選択したリスト項目を取得し、それらを配列リストに追加するのを手伝ってください。可能であれば、有用なリンクも提供してください。これまでのコードは次のとおりです (Blackberry アプリケーション開発は初めてです)。助けてください。

package mypackage;
import java.util.Vector;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.IntVector;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen implements ListFieldCallback
{
    private Vector _listData = new Vector();
    private Vector _checkedData = new Vector();
    private ListField listField;
    private static final String[] _elements = {"First element", "Second element","Third element"
    };
    //private static final String[] _elements1 = {"hai","welcome","where r u"
    //};
    private MenuItem _getDataMenu,selectall,Delete;
    Vector result = new Vector();
    protected void makeMenu(Menu menu, int instance)
    {
        menu.add(_getDataMenu);
        menu.add(selectall);
        menu.add(Delete);
        //Create the default menu.
        super.makeMenu(menu, instance);
    }

    private class ChecklistData
    {
        private String _stringVal;
        private boolean _checked;

        ChecklistData(String stringVal, boolean checked)
        {
            _stringVal = stringVal;
            _checked = checked;
        }

        //Get/set methods.
        private String getStringVal()
        {
            return _stringVal;
        }

        private boolean isChecked()
        {
            return _checked;
        }
        //Toggle the checked status.
        private void toggleChecked()
        {
            _checked = !_checked;
        }

    }
    public Vector getCheckedItems() {      
        return _checkedData;   
    } 

    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {    


        // Set the displayed title of the screen       
        setTitle("MyTitle");

        VerticalFieldManager main = new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT|
                VerticalFieldManager.USE_ALL_WIDTH|VerticalFieldManager.VERTICAL_SCROLL);
        this.add(main);
        HorizontalFieldManager hfm = new HorizontalFieldManager();
        main.add(hfm);
        listField = new ListField(){
            //Allow the space bar to toggle the status of the selected row.
            protected boolean keyChar(char key, int status, int time)
            {

                boolean retVal = false;

                //If the spacebar was pressed...
                if (key == Characters.SPACE)
                {
                    //Get the index of the selected row.
                    int index = getSelectedIndex();

                    //Get the ChecklistData for this row.
                    ChecklistData data = (ChecklistData)_listData.elementAt(index);

                    //Toggle its status.
                    data.toggleChecked();

                    //Update the Vector with the new ChecklistData.
                    _listData.setElementAt(data, index);

                    //Invalidate the modified row of the ListField.
                    invalidate(index);

                    //Consume this keyChar (key pressed).
                    retVal = true;
                }
                return retVal;
            }
        };

        listField.setCallback(this);

        reloadList();
        int elementLength = _elements.length;
        for(int count = 0; count < elementLength; ++count)
        {
            _listData.addElement(new ChecklistData(_elements[count], false));
            //_listData.addElement(new ChecklistData(_elements1[count], false));
            listField.insert(count);
        }

        main.add(listField);
        _getDataMenu =new MenuItem("Get Data", 200, 10) {
            public void run(){
                int index = listField.getSelectedIndex();   
                ChecklistData data = (ChecklistData)_listData.elementAt(index); 

                String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();
                //Dialog.alert(message);       
                // get all the checked data indices   
                IntVector selectedIndex = new IntVector(0, 1);     
                //ChecklistData data;   
                for (int i=0;i<_listData.size();i++) {         
                    data = (ChecklistData)_listData.elementAt(i);
                    if(data.isChecked()) {    
                        selectedIndex.addElement(i);  
                        String selectedvalues = data.getStringVal();
                        System.out.println("Selected items are:"+selectedvalues);
                    }    
                }   
                data = null;   


                // now selectedIndex will contain all the checked data indices.
                //String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();

            }
        };

        selectall = new MenuItem("Selectall", 200, 10){
            public void run(){

                int elementLength = _elements.length;
                for(int count = 0; count < elementLength; ++count)
                {
                    _listData.setElementAt(new ChecklistData(_elements[count], true), count);

                }
            }
        };
        Delete = new MenuItem("Delete", 200, 10){
            public void run(){
                int index = listField.getSelectedIndex();
                _listData.removeElementAt(index);

                // update the view
                listField.delete(index);
                listField.invalidate(index);
            }

        };


    }
    private void reloadList() {
        // TODO Auto-generated method stub
        _listData.setSize(_listData.size());
    }
    public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) 
    {
        ChecklistData currentRow = (ChecklistData)this.get(list, index); 
        StringBuffer rowString = new StringBuffer();


        if (currentRow.isChecked())
        {
            rowString.append(Characters.BALLOT_BOX_WITH_CHECK);

        }
        else
        {
            rowString.append(Characters.BALLOT_BOX);
        }
        //Append a couple spaces and the row's text.
        rowString.append(Characters.SPACE);
        rowString.append(Characters.SPACE);
        rowString.append(currentRow.getStringVal());


        //Draw the text.
        graphics.drawText(rowString.toString(), 0, y, 0, w);
        /*if (currentRow.isChecked()) {   
            if( -1 ==_checkedData.indexOf(currentRow))     
                _checkedData.addElement(currentRow);   
            rowString.append(Characters.BALLOT_BOX_WITH_CHECK); 
        } 
        else {  
            if( -1 !=_checkedData.indexOf(currentRow))    
                _checkedData.removeElement(currentRow);  
            rowString.append(Characters.BALLOT_BOX);
        } */
    }
    //Returns the object at the specified index.
    public Object get(ListField list, int index) 
    {

        return _listData.elementAt(index);
    }
    public int indexOfList(ListField list, String p, int s) 

    {
        //return listElements.getSelectedIndex();
        return _listData.indexOf(p, s);
    }
    //Returns the screen width so the list uses the entire screen width.
    public int getPreferredWidth(ListField list) 
    {
        return Display.getWidth();
    }




    protected boolean navigationClick(int status, int time) {
        int index1 = listField.getSelectedIndex();
        /*System.out.println("Selected item index:"+index1);
        //int[] list =listField.getSelection();
        //String s = Integer.toString(list);
        System.out.println(" items are:"+_elements[index1]);
        //ChecklistData data = (ChecklistData)_listData.elementAt(index1);*/

        //Get the ChecklistData for this row.
        ChecklistData data = (ChecklistData)_listData.elementAt(index1);
        String message = "Selected data: " + data.getStringVal() + ", and status: " + data.isChecked();
        System.out.println("message is:"+message);

        //Toggle its status.
        data.toggleChecked();

        //Update the Vector with the new ChecklistData.
        _listData.setElementAt(data, index1);

        //Invalidate the modified row of the ListField.
        listField.invalidate(index1);

        return true;
    }

}
4

0 に答える 0