⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 articlesscreen.java

📁 In the last three articles, I’ve been walking you through the creation of an end-to-end BlackBerry a
💻 JAVA
字号:
package KnowledgeBase;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.Bitmap;
import javax.microedition.io.HttpConnection;
import java.util.Vector;
import net.rim.device.api.util.StringUtilities;

class ArticlesScreen extends MainScreen implements HTTPTransportListener {
    
    private Article[] articles;
    private ArticlesListField articlesList;
    private ArticleScreen articleScreen;
    
    private static final int ACT_SEARCH_ARTICLES = 1;
    private static final int ACT_GET_ARTICLES_BY_TAG = 2;
    
    private static String KEY_ACTION = "act";
    private static String KEY_SEARCH_PHRASE = "sp";
    private static String KEY_TAG = "tg";
    
    private static String FIELD_SEPARATOR = "^~^";
    private static String RECORD_SEPARATOR = "~^~";
    
    private MenuItem viewMenu = new MenuItem("Open",100,10) {
       public void run() {
           viewArticle();
       } 
    };
    
    ArticlesScreen() {
        
        this.setTitle("Articles");
        
        /*
        articles = DataStore.getCachedArticles();
        
        if (null == articles || articles.length == 0) {
        
            // Create a few dummy articles in order to test the screen;
            articles = new Article[15];
            Article article;
            for (int i = 0; i < 15; i++) {
                article = new Article();
                article.title = "Dummy article " + Integer.toString(i);
                article.author = "ramonj";
                article.contents = "This is a test article";
                article.tags = new String[] {"tag 1", "tag 2", "tag 3"};
                article.dateCreated = "01/01/2008";
                articles[i] = article;
            }
            
            DataStore.setCachedArticles(articles);
            
        }
        */
        
        articlesList = new ArticlesListField();
        //articlesList.set(articles);
        this.add(articlesList);
        
    }
    
    protected boolean navigationClick(int status, int time) {
        Field focus = this.getLeafFieldWithFocus();
        if (focus == articlesList) {
            viewArticle();
            return true;
        }
        return false;
    }
    
    protected void makeMenu(Menu menu, int instance) {
        menu.add(viewMenu);
        menu.add(MenuItem.separator(viewMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
    
    private void  viewArticle() {
        int selected = articlesList.getSelectedIndex();
        if (selected > -1) {
            Article selectedArticle = (Article)articlesList.get(articlesList,selected);
            if (null == articleScreen) {
                articleScreen = new ArticleScreen();
            }
            articleScreen.showArticle(selectedArticle);
            UiApplication.getUiApplication().pushScreen(articleScreen);
        }
    }
    
    public void searchArticles(String searchPhrase) {
        
        String[] keys = new String[] {KEY_ACTION, KEY_SEARCH_PHRASE};
        String[] values = new String[] {String.valueOf(ACT_SEARCH_ARTICLES),searchPhrase};   
        sendHttpRequest( keys, values);
        
    }
    
    public void getArticlesByTag(String tag) {
        
        String[] keys = new String[] {KEY_ACTION, KEY_TAG};
        String[] values = new String[] {String.valueOf(ACT_GET_ARTICLES_BY_TAG),tag};   
        sendHttpRequest( keys, values);
        
    }
    
    private void sendHttpRequest(String[] keys, String[] values) {
        
        String requestContents = TransportUtils.createRequestString(keys, values);
        
        String url = DataStore.getAppServerUrl();
        
        // Make sure we can create an http request.
       // If the app. server URL has not been set, we cannot make any http requests.
        if (null == url || url.length() == 0) {
            Dialog.alert("Invalid application server url. Please correct this problem in the Options Screen.");
            return;
        }
        
        // Create a mock HTTPTransport to simulate sending a request to our
        // serrver side application.
        MockHTTPTransport transport = new MockHTTPTransport();
        //HTTPTransport transport = new HTTPTransport();
        transport.setHTTPTransportListener(this);
        transport.send(KbHTTPTransport.REQ_METHOD_POST, url, requestContents);
    }
    
    public void processResponse(final int HTTPResponseCode, final String data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
                switch (HTTPResponseCode) {
            
                    case (KbHTTPTransport.NOT_IN_COVERAGE): 
                        Dialog.alert("Not in coverage.");
                        break;
                    case HttpConnection.HTTP_OK:
                        parseResponseData(data);
                        break;
                    // TODO: Check all the http error codes you are interested in.
                    // for example HttpConnection.HTTP_FORBIDDEN
                    default:
                    Dialog.alert("There was an error with the request.");
                    break;
                        
                    }
            }
        });
        
    }
    
    private void parseResponseData(String data) {
        
        // Parse out each article from the string data
        // Format: id^~^title^~^dateCreated^~^author^~^
        //tag1,tag2,...^~^contents~^~<NEXT RECORD>
        
        Vector articlesVector = new Vector();
        int index = 0;
        int length = data.length();
        while (index != -1 && index < length - 1){
            
            if (index == 0) index = -1;
            
            Article article = new Article();
              
            article.id = data.substring(++index , 
                    index = data.indexOf(FIELD_SEPARATOR, index));
            index += 3;
            article.title = data.substring(index , 
                    index = data.indexOf(FIELD_SEPARATOR, index));
            index += 3;
            article.dateCreated = data.substring(index, 
                    index = data.indexOf(FIELD_SEPARATOR, index));
            index += 3;
            article.author = data.substring(index, 
                    index = data.indexOf(FIELD_SEPARATOR, index));
            index += 3;
            String tags = data.substring(index, 
                    index = data.indexOf(FIELD_SEPARATOR, index));
            article.tags  = StringUtilities.stringToWords(tags);
            index += 3;
            article.contents = data.substring(index, 
                    index = data.indexOf(RECORD_SEPARATOR, index)); 
            index += 2;           
            articlesVector.addElement(article);
        }
        
        articles = new Article[articlesVector.size()];
        articlesVector.copyInto(articles);
        articlesList.set(articles);        
    }
    
    private class ArticlesListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/document.png");                
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                    
            if (null != icon) {
                int offsetY = (this.getRowHeight() - icon.getHeight())/2;
                graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                        icon.getHeight(),icon,0,0);
                graphics.drawText(articles[index].title + " (" + articles[index].dateCreated + ")", 
                    icon.getWidth() + 2, y, DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            } else {
                graphics.drawText("- " + articles[index].title + " (" + articles[index].dateCreated + ")", 0,
                    y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("- "));
            }
        }
    };
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -