I need solution for this...

Hi all... I am a beginner in blackberry development.

The code below is every time we click the ok button, it retrieves the content of the

Saran.txt the SD card file and displays them on the screen. So far so good, but the problem

is whenever I click on ok again, the content is displayed again. I don't want it to be

displayed again instead, I want that he should be replaced. I am facing this kind of problem with all areas

that appears on the screen, that is to say, the second time I click the button only one copy of this field is

display again.

can someone suggest a solution for this...

package myText;

import java.io.*;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.PNGEncodedImage;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.*;

public class myText extends UiApplication
{
    public static void main(String[] args) throws IOException
    {
        myText theApp = new myText();
            theApp.enterEventDispatcher();
    }
    public myText() throws IOException
    {
            new Textdisplay();
    }
}
final class Textdisplay implements FieldChangeListener
{
    MainScreen mainScreen=new MainScreen();
    ButtonField ok=new ButtonField("OK");
    //HttpConnection hc;
    //String url = "http://wordpress.org/extend/plugins/about/readme.txt";
    //InputStream is=null;
    public Textdisplay() throws IOException
    {
        UiApplication.getUiApplication().pushScreen(mainScreen);
        mainScreen.setTitle("Text file Fetching......");
        mainScreen.add(ok);
        ok.setChangeListener(this);
    }
    /*public void downloading() throws IOException
    {
        ConnectionFactory connFact = new ConnectionFactory();
        ConnectionDescriptor connDesc;
        connDesc = connFact.getConnection(url);
        if (connDesc != null)
        {
            HttpConnection httpConn;
            httpConn = (HttpConnection)connDesc.getConnection();
            try
            {
                final int iResponseCode = httpConn.getResponseCode();
                 Dialog.alert("Response.. code: "+Integer.toString(iResponseCode));
                 mainScreen.add(new LabelField(httpConn.getHost()+"\n"+httpConn.getRequestMethod()));
                 is=httpConn.openInputStream();
                 StringBuffer buffer= new StringBuffer();
                 int chars;
                 while((chars = is.read()) != -1)
                 {
                    buffer.append((char) chars);
                 }
                 String text = new String(buffer);
                 mainScreen.add(new LabelField(text));
             }
             catch (IOException e)
             {
               System.err.println("Caught IOException: "
                    + e.getMessage());
             }
        }
    }
    */
    public void Fetching() throws IOException
    {
        String path = "file:///SDCard/BlackBerry/textfiles/saran.txt";
        FileConnection FileConn=null;
        InputStream is=null;
        TextField label = null;

        int chars;

        Dialog.alert("entering");
        try{
            FileConn = (FileConnection) Connector.open(path,Connector.READ);
            Dialog.alert("file connection");
            if(!FileConn.exists()){
                FileConn.create();
            }
            Dialog.alert("connection created");
            //int length = (int) FileConn.fileSize();
            //byte[] data = new byte[length];
            is = FileConn.openInputStream();
            //dis.readFully(data);
            Dialog.alert("not yet");
            StringBuffer buffer= new StringBuffer();
            while((chars = is.read()) != -1)
             {
                buffer.append((char) chars);
             }
             String text = new String(buffer);
             Dialog.alert("Click to read data.......");
             label.setText(text);
            mainScreen.add(label);
            is.close();
        }
        catch(Exception e){
            System.out.println("file not found:" + e);
            Dialog.alert("file not found"+e);
        } finally {
            try{ FileConn.close(); } catch(Exception e){}
        }
    }
    public void fieldChanged(Field f, int context)
    {
        try
        {
            //Dialog.alert("Fetching the file.......");
            mainScreen.add(new LabelField("Fetching the data..........."));
            Fetching();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

In your current code, ButtonField 'ok' is global, so you can refer to this anywhere.  Add a BitmapField which is overall too.  And then in your transformation, set this to the new Bitmap that you created and invalidate the screen (mainScreen.invalidate ()).

If you add a BitmapField and it doesn't have a bitmap, it will not display.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for