Thank you for reply,,

I want to save the history for my application in a file on sdcard.

First i tried to save it as xml and parse it to load...but these actions
take a lot of time..(almost 8 seconds for 20 strings on a HTC Hero device);

this is the code:

package com.app.lookitup2;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.util.ByteArrayBuffer;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class HistoryManager
{

    private ArrayList<HistoryItem> history_items;
    private Context      context;
    private final String WHAT_TAG_START  = "<what>";
    private final String WHAT_TAG_END    = "</what>";
    private final String WHERE_TAG_START = "<where>";
    private final String WHERE_TAG_END   = "</where>";
    private final String DATE_TAG_START  = "<date>";
    private final String DATE_TAG_END    = "</date>";
    private final String ITEM_TAG_START  = "<item>";
    private final String ITEM_TAG_END    = "</item>";


    public  HistoryManager(Context ctx)
    {
        context = ctx;

        history_items = new ArrayList<HistoryItem>();
    }


    public void addHistoryItem(HistoryItem h_item)
    {
        history_items.add(h_item);
    }

    public HistoryItem getHistoryItem(int position)
    {
        return history_items.get(position);
    }

    public boolean saveHistory()
    {

        String data;
        int i;

        File root = Environment.getExternalStorageDirectory();
        File history_file = new File(root,"history_lookitup.xml");
        if(history_file.length() >= 1048576)
        {
            Log.e("SAVE_HISTORY","History is full!Please empty!");
            return false;
        }

        data =  new String("");

        for(i = 0; i < history_items.size(); i++)
        {
            data = data + ITEM_TAG_START +
                            WHAT_TAG_START +
history_items.get(i).getWhat().getText().toString() + WHAT_TAG_END +
                            WHERE_TAG_START +
history_items.get(i).getWhere().getText().toString() + WHERE_TAG_END +
                            DATE_TAG_START +
history_items.get(i).getDate().toString() + DATE_TAG_END +
                        ITEM_TAG_END;
        }

        try
        {
            if(root.canWrite())
            {
                FileWriter history_writer = new
FileWriter(history_file,true);
                BufferedWriter out = new BufferedWriter(history_writer);

                out.write(data);
                out.close();
                Log.e("SAVE HISTORY","History is saved");
                return true;
            }
            else
            {
                Log.e("SAVE HISTORY","no sd card or no permission to write
on it");
                return false;
            }
        } catch (IOException e)
        {
            Log.e("WRITE FILE","Could not write file " + e.getMessage());
            return false;
        }

    }

    public boolean loadHistory() throws IOException
    {
        String myString = " ";
        ArrayList<HistoryItem> items;


        FileInputStream is;
        try {

            is = new FileInputStream("/sdcard/history_lookitup.xml");
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while((current = bis.read()) != -1)
               baf.append((byte)current);

            myString = new String(baf.toByteArray());

            if(myString.equals(" ") == false)
            {
                HistoryParser parser = new HistoryParser("<?xml
version='1.0' encoding='UTF-8'?><main>" + myString + "</main>",context);

                items = new ArrayList<HistoryItem>();
                items = parser.getItemList();
                Log.e("FIRST_HISTORY", items.get(0).getDate().toString());
                Log.e("DATA",myString);

                history_items = items;
                Log.e("history_items =
",Integer.toString(history_items.size()));
                return true;
            }
            else
                return false;


        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
            return false;
        }


    }

    public int getCount()
    {
        return history_items.size();
    }

    public ArrayList<HistoryItem> getHistoryList()
    {
        return history_items;
    }

    public void clearHistory() throws IOException
    {
        File root = Environment.getExternalStorageDirectory();
        File history_file = new File(root,"history_lookitup.xml");

        if(root.canWrite())
        {
            FileWriter history_writer = new FileWriter(history_file);
            BufferedWriter out = new BufferedWriter(history_writer);

            out.write(" ");
            out.close();
            Log.e("Clear HISTORY","History is empty now");
            loadHistory();
        }
    }

I tried to use sharedPreference ..i put all the strings in preference:the
saving time was good..but when i try to load it i got same problem...

Thank you


}


>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to