×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    159

    please help,how to use selection listener in list in lwuit for S40

    hello,I am working on S40 using sdk2.0 and lwuit 1.5.I am using List and added string items in list and show on form indexwise .now i want to perform on click event on a perticular index how is it possible .sending snapshot as i required http://imgur.com/e9MOeaY and please tell me how we can remove all items from list

    please suggest me
    thanks
    Last edited by vikranttanwar@; 2013-08-02 at 13:31.

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: please help,how to use selection listener in list in lwuit for S40

    use list.addActionListener(ActionListener listener);
    and then in the ActionListener.actionPerformed() method you can query the list.getSelectedIndex() or list.getSelectedItem()

    to "remove" all the items from a list you simply remove them from its Model
    you can remove one by one using list.getModel().removeItem(index)
    or If you used the DefaultListModel then you can cast it to it and use its removeAll() method
    ((DefaultListModel)list.getModel()).removeAll();

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    159

    Re: please help,how to use selection listener in list in lwuit for S40

    please help me with a simple example

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: please help,how to use selection listener in list in lwuit for S40

    there is nothing more to what shai has mentioned , but just to make it more clear:

    Code:
    class MyList implements ActionListener {
    
    public MyList() {
    this.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e) {
    int selectedIndex = ((MyList)e.getSource()).getSelectedIndex();
    // now access your model and remove all from your model
    }
    
    }
    Hope this helps!!

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    159

    Re: please help,how to use selection listener in list in lwuit for S40

    but when i use the code
    Code:
    System.out.println("size of list   for test::"+l.size());
    					for(int i=0;i<l.size();i++)
    					{
    						
    						l.getModel().removeItem(i);
    					}
    					System.out.println("size of list   for test after remove item::"+l.size());
    Before the remove code my list size is 4 but after apply this code ,no all items remove after remove item my list size remaine 2 not zero .how it is so happened???
    i did't use any list mode i used only List list=new List();
    please help me
    Last edited by vikranttanwar@; 2013-08-06 at 07:11.

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: please help,how to use selection listener in list in lwuit for S40

    if l is your list object then i think you are doing wrong it should be like:

    Code:
    System.out.println("size of list   for test::"+l.getModel().size());
    					for(int i=0;i<l.getModel().size();i++)
    					{
    						
    						l.getModel().removeItem(i);
    					}
    					System.out.println("size of list   for test after remove item::"+l.getModel().size());

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    159

    Re: please help,how to use selection listener in list in lwuit for S40

    nothng help from this .same result is there after applying this

  8. #8
    Regular Contributor
    Join Date
    Dec 2012
    Location
    India
    Posts
    271

    Re: please help,how to use selection listener in list in lwuit for S40

    Hi.Try this code.

    Code:
    System.out.println("Length is :" + l.getModel().getSize());
    int len = l.getModel().getSize();
    for (int it = 0; it < len; it++) {
    System.out.println("Removing element "+l.getModel().getItemAt(0));
    l.getModel().removeItem(0);
    }
    System.out.println("Length is :" + l.getModel().getSize());

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    159

    Re: please help,how to use selection listener in list in lwuit for S40

    thanx arunkam .
    it is working properly.BUT can u tell me how it is possible;
    l.getModel().removeItem(0);
    tellme about zero index.why we call zero again and again instead of int it=0;

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    159

    Re: please help,how to use selection listener in list in lwuit for S40

    thanx gaba88
    BUt this code delete those items which is on even index not from all index. I tried it properly.
    Last edited by vikranttanwar@; 2013-08-06 at 12:36.

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,737

    Re: please help,how to use selection listener in list in lwuit for S40

    It is a classic kind of side-effect. A quick-fix could be reversing the iteration, e.g.
    Code:
    for(int it=len-1;it>=0;it--)

  12. #12
    Regular Contributor
    Join Date
    Dec 2012
    Location
    India
    Posts
    271

    Re: please help,how to use selection listener in list in lwuit for S40

    Hi Vikrant

    Basically , whenever you remove an entry from the List Model of a List, the indexes of the entries in the Model are automatically updated. Hence if u have 4 items and if u delete the 2nd element , the indexes will not be 0,2,3 but 0,1,2.
    Code:
        for(int i=0;i<l.getModel().size();i++) 
       {
    	   l.getModel().removeItem(i);
        }
    Your loop's condition was the problem.The condition being queried for each time is the Size of the List (Model). This will vary each time as you are removing entries from the list in the body of the loop.Hence the condition will fail after the second time the loop is executed.

    That is why I have obtained the length of the List in a seperate variable.

    Removing the element at the zeroth location for "len" number of times was a sure shot way of making sure that all elements are removed.

    But as Wizard_Hu suggested, a better approach would be to run the iteration backwards.

    Code:
     for (int it = len-1; it >= 0; it--) {
                System.out.println("Removing element "+l.getModel().getItemAt(it));
                l.getModel().removeItem(it);
            }

Similar Threads

  1. How to append LWUIT List screen to a LWUIT Form in j2me?
    By pavanragi in forum Mobile Java General
    Replies: 1
    Last Post: 2012-08-06, 07:01
  2. How to apply listener to LWUIT table
    By rajan701v in forum Mobile Java General
    Replies: 1
    Last Post: 2011-06-28, 14:15
  3. LWUIT multiple selection list
    By senthil_25 in forum Mobile Java General
    Replies: 2
    Last Post: 2010-11-12, 10:35
  4. Image list with listener
    By dannemannen in forum Mobile Java General
    Replies: 4
    Last Post: 2009-03-26, 08:38
  5. Replies: 0
    Last Post: 2008-11-10, 10:32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×