0

Here is the sample code:

class MailBoxSampleListField extends MainScreen implements FolderListener, StoreListener {
private static final int COLUMN_WIDTH_STATUS = 10;
private static final int COLUMN_WIDTH_DATE = 150;
private static final int COLUMN_WIDTH_NAME = 150;

public ListField myList; 
private ListCallback myCallback;
public Vector sampleList = new Vector();
public Vector sampleVector;

    private class ListCallback implements ListFieldCallback {
    public Vector myVector = new Vector();
    public Bitmap LIST_IMAGE = Bitmap.getBitmapResource("New.PNG");
    public void drawListRow (ListField list, Graphics g, int index, int y,int w) {
        displayList(g,0,y,w,(( Message )myVector.elementAt( index )), LIST_IMAGE); // for drawing the list row
        for( int ii = 0; ii < sampleVector.size(); ii++) {
            String text = ( String )sampleVector.elementAt(ii);
            int liney = y + ( ii * list.getFont().getHeight() );
            g.drawText( text, LIST_IMAGE.getWidth() + 5, liney, Graphics.ELLIPSIS, w );
        }
    }

    public Object get( ListField list, int index ) {
        return myVector.elementAt(index); 
    }

    public int indexOfList( ListField list,String p, int s ) {
        return myVector.indexOf(p,s);
    }

    public int getPreferredWidth ( ListField list ) {
        return Graphics.getScreenWidth();
    }

    public void insert(Message _message, int index) {
        myVector.addElement(_message);
    }

    public void erase () {
        myVector.removeAllElements();
    }
}

MailBoxSampleListField() {

    ListCallback myCallback = new ListCallback(); 

    try {
        Store store = null;
        store = Session.getDefaultInstance().getStore();
        store.addStoreListener( this );

        // retrieve Folder object fow which we want to receive message notification
        try {
            Folder[] folders = store.list();
            Folder[] f1 = store.findFolder( "inbox" );
            Folder vinbox = f1[0];
            for (int i =0; i < f1.length; i++) {
                f1[i].addFolderListener( this );
            }

            Message[] vmessages = vinbox.getMessages();
            for ( int j = 0; j < vmessages.length; ++j ) {
                if(vmessages[j] != null){
                    sampleList.addElement( vmessages[j] );
                }
            }
            myList = new ListField(); // initialize the ListField
            for ( int k = 0; k < sampleList.size(); k++ ) {
                myList.insert(k);
                myCallback.insert(vmessages[k], k);   
            }

            myList.setCallback( myCallback );
            add( myList );     
        }
        catch( Exception e ){
        }
    }
    catch ( Exception se ) {
    }
}

public void displayList( Graphics g, int x, int y, int width, Message _message, Bitmap LIST_IMAGE ) {
    g.drawBitmap(0, y, LIST_IMAGE.getWidth(), LIST_IMAGE.getHeight(), LIST_IMAGE, 0, 0);
    sampleVector = new Vector();
    Date d = _message.getReceivedDate();
    Calendar c = Calendar.getInstance();
    c.setTime(d);
    StringBuffer sb = new StringBuffer();
    sb.append( c.get( Calendar.MONTH ) );
    sb.append('-');
    int digit = c.get( Calendar.DAY_OF_MONTH );
    if ( digit < 10 ) sb.append(0);
    sb.append( digit );
    sb.append(' ');
    sb.append( c.get( Calendar.HOUR_OF_DAY ) );
    sb.append(':');
    digit = c.get( Calendar.MINUTE );
    if ( digit < 10 ) sb.append( 0 );
    sb.append( digit );
    sb.append( ' ');

    x += LIST_IMAGE.getWidth()+5; 
    x += COLUMN_WIDTH_DATE;

    try {
        String name = "<noname>";
        if ( _message.isInbound() ) {
            Address a = _message.getFrom();
            if ( a != null )
            {
                name = a.getName();
                if ( name == null || name.length() == 0 ) name = a.getAddr();
            }
        }
        else
        {
            //get the first Recipient address
            Address[] set = _message.getRecipients(Message.RecipientType.TO);
            if ( set != null && set.length > 0 ) 
            {
                name = set[0].getName();
                if ( name == null || name.length() == 0 ) name = set[0].getAddr();
            }
        }           
       sampleVector.addElement(name +"   " + sb.toString());
        } catch (MessagingException e) {
        System.err.println(e);
    } 

    x += COLUMN_WIDTH_NAME;
    int remainingColumnWidth = Graphics.getScreenWidth() - x;

    //get the subject, or if that doesn't exist, the first line of the body
    String textToDisplay = _message.getSubject();
    if ( null == textToDisplay) //no subject! get the first line of the body if present
    {
        Object o = _message.getContent();
        if ( o instanceof String )
        {
            textToDisplay = (String)o;
        }
        else if ( o instanceof Multipart )
        { 
            Multipart mp = (Multipart)o;
            int count = mp.getCount();
            for (int i = 0; i < count; ++i)
            {
                BodyPart p = mp.getBodyPart(i);
                if ( p instanceof TextBodyPart )
                {
                    textToDisplay = (String)p.getContent();
                }
            }
        }
    }
            sampleVector.addElement(textToDisplay);
    } public void messagesAdded(FolderEvent e) {} public void messagesRemoved(FolderEvent e) { } public void batchOperation( StoreEvent se) { }

}

4
  • Posting your code within the [Code] tag would be great stackoverflow.com/editing-help
    – Michael B.
    Feb 16, 2010 at 3:46
  • I just changed the code. please advice
    – Pankaj
    Feb 19, 2010 at 13:11
  • 1
    I, and most of the human race, am lazy and will not read all that... can you post a simpler example that illustrates the problem?
    – roryf
    Feb 19, 2010 at 13:13
  • AFAIK you must to implement the text wrapping or the replace the text with "..." if it is too long.
    – Alex
    Mar 17, 2010 at 14:27

4 Answers 4

0

I'm not sure what you mean, could you please post a screenshot so that we can see the problem?

I'll try to help out the best I can. In my limited experience, I have noticed that in a listfield, if you have not set the row height (with setRowHeight()) to a big enough height, graphics (including text) that overflow over the size of the row will not be displayed. Have you tried setting the row height to 2 * list.getFont().getHeight() or more?

0

If not all rows are displayed, then I think you've missed to call myList.setSize(myVector.size());

I'm not sure what you mean by "wrapping not happening"...

The drawListRow() will be called repeatedly (for the amount of times set by the setSize() that I've suggested above).

In your current code you iterate through the whole myVector on each drawListRow() call - this is wrong.

0

You must use the value y which is declare in drawListRow (ListField list, Graphics g, int index, int y,int w)

like

g.drawText( text, LIST_IMAGE.getWidth() + 5, y+liney, Graphics.ELLIPSIS, w )

I am facing this issue for long time finally i got the solution.

0

As Alex say, you need to implement your own wrapper class, something that looks like:

    TextWrapper theWrapper = new TextWrapper();

    String[] wrappedText = theWrapper.textWrap(longText, wrappingWidth , 2);

    //
    // now draw text line by line
    //
    g.drawText(wrappedText[0], x, y, DrawStyle.LEFT, width);
    if (wrappedText.length > 1) {
        g.drawText(wrappedText[1], x, y + textFont.getHeight(), DrawStyle.LEFT | DrawStyle.ELLIPSIS, width);
    }

where

public class TextWrapper {

    ... // put here methods used by textWrap method

    //
    // textWrap splits input String in lines having width as maxWidth
    //
    public String[] textWrap(String s, int maxWidth, int maxLines)
    {
        String[] result;

        ... // do here the wrap job on input string s

        return result;
    }     
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.