0

EDIT: SOLVED AS IM A TOTAL FOOL and didnt notice the file name was wrong between my 2 activities. Never mind, thanks to all those that helped. Still learning, just hope i can stop re-learning very old lessons!

im new to android and java and am trying to do what seems simple, but am struggling and am not sure why.

I want to load a string from a text file and extract the numbers from the string as an int for use elsewhere in my app. I want to use the int then in an array for use with androidplot to draw a graph, i have the graph working with a manual array (ie just type some random numbers in myself) so have omitted most of that code from here for simplicity.

This is my code for the load fo the txt file, split it at " " and then parse the int, but it isnt working.

     //actually load the progress file
public void loadDatacomm() {

        //load the asset file
            StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader("/sdcard/.progress/1gppi.txt"));
            String line;

        while ((line = br.readLine()) != null) {
            String line2[] = line.split(" ");  //split the string to get the progress
            commprogress1 = Integer.parseInt(line2[0]); 
                }

        }
        catch (IOException ex) {
            return;
        }

The only other bit of interest i guess is the array in android plot which i am building like this;

    Number[] commprogress = {commprogress1, commprogress2, commprogress3, commprogress4, commprogress5, commprogress6, commprogress7, commprogress8, commprogress9, commprogress1};

When i load the activity then the graph doesnt show any value, if i manually declare values of the commprogress1, 2, 3, then the graph draws fine.

Can someone help me please?

EDIT the file i am trying to load contains the following:

30 %complete

It is written by another activity in the application.

I have no idea why the code isnt working, I am using similar code elsewhere in the application to extract the int (in this case 30) from the split string, but its not working here.

andy
  • 391
  • 13
  • 33
  • 1
    Can you perhaps edit the question to clarify a few things such as: Do you have any idea where is the code failing? Can you read the lines properly? What is the value parsed from Integer.parseInt(line2[0]); Can you give an extract of the file providing the data? – Thomas Jan 03 '13 at 13:15
  • But can you describe what is not working? Do you get an exception when trying to read the file? Do you get wrong data, does it crash (if so, what are the logs)? – Thomas Jan 03 '13 at 13:42
  • From what information you've provided, I think you're doing it wrong. If Activity A wants to send some info to Activity B, then there are better approaches to do it. – Sudarshan Bhat Jan 03 '13 at 13:44
  • The app doesnt crash, it just doesnt draw the value from the txt file on the androidplot graph i have running. Its just as if the code above hasnt attached any value to the int commprogress1 so the graph is just drawn with 0 plotted. – andy Jan 03 '13 at 13:47
  • can you check if the code on the while loop is not called more than once? Is there any chance that you are overwriting the commprogress1? Though, as I mentioned in the answer below, Shared Preferences may be the preferred way to go for your needs – Thomas Jan 03 '13 at 13:52
  • Ok, so bit of a hack to check but i manually set the value of commprogress1 as 10 (public int commprogress1=10;) then added a toast to display the value of commprogress1 after the loadDatacomm(); and the value of commprogress displayed was 10, so the while loop doesnt seem to be writing anything at all to the file. It is only called once as far as i can see. – andy Jan 03 '13 at 13:58
  • so the problem is on the activity which writes in the file? – Thomas Jan 03 '13 at 14:04
  • The file is written correctly, i am loading it elsewhere and it is fine, i have also tried manually making the text file with 30 %complete and am getting the same issue. If i place a toast under the try{ and above the bufferedReader then it displays but strangly if i place a toast under the String line; then its not displayed. Is there anything wrong with my bufferedReader line? – andy Jan 03 '13 at 14:10
  • IM SOOOOO SORRY GUYS AND GIRLS, im a total idiot. I just rechecked everything and the bloddy filename is incorrect between the 2 activities, hence why its not working! Thanks for your help! – andy Jan 03 '13 at 14:15
  • yeah, you were probably getting an exception, but since you are not printing anything on the catch clause, you were not being notified about it. In any case, Id recommend looking at the shared preferences =D – Thomas Jan 03 '13 at 14:19

1 Answers1

0

In case you are getting an exception when trying to access the file, it could be due to the concurrency as the other application is writing into it (though since you are just reading the file, Id expect it not to be a problem).

In any case, if you want just to share the percentage in between activities, I think that the use of shared preferences may be more suitable

Thomas
  • 2,751
  • 5
  • 31
  • 52
  • Sorry, i miss understood what you were asking for. I dont get any exception reading from the file and the other activity is not accessing the file at the same time. I want to have the % saved regardless of the state of the app/battery etc so figured writing it to the txt file was easiest. – andy Jan 03 '13 at 13:45
  • Try to look into the Shared Preferences (http://developer.android.com/guide/topics/data/data-storage.html#pref) it is a very straight forward way to share simple data between activities. I think it may address your needs better. But for the problem retrieving the data from the file, we would need a bit more context information (like what is the value of commprogress1, or to know if you are not overwriting it in case the file has more than a line, etc) – Thomas Jan 03 '13 at 13:50