Grid View and Text File


Last Updated:

  1. Beanstalk

    Beanstalk Member This Topic's Starter

    Joined:
    Jun 24, 2010
    Messages:
    9
    Likes Received:
    0
    Hello,

    Gridview

    I have used the GridView layout just like in: GridView | Android Developers
    What I want to implement is a way to update the images present in this grid when an item is clicked. The array I'm writing the images based on is updating, but my View is not changing. It's still painting the original images regardless of the new contents of the array. How do I make it dynamically update the image it displays?

    Text Files

    I was wondering if anyone could link me to a sample program on how to read line-by-line from a text file.

    Randomize

    Lastly, is there an inbuilt function to randomly a number select a number in a specific range?

    Thanks in advance, and sorry for the slightly basic questions, I'm just starting out =]
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    If the text file is on your sdcard, you might have something like this:

    Code (Text):
    1. public String readFile()
    2. {
    3.     File txtFile = new File(filePath);
    4.     BufferedReader input;
    5.    
    6.         try
    7.         {
    8.             input = new BufferedReader(new FileReader(txtFile));
    9.             String line;
    10.             line = input.readLine();
    11.             input.close();
    12.         }
    13.         catch(IOException e)
    14.         {
    15.             //handle exceptions here
    16.         }
    17.        
    18.     return line;
    19. }
    That will read a single line of code. You could put that into a loop to read multiple lines.
     
    Beanstalk likes this.
  3. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I'm not sure about the gridview -- you're updating the list adapter, not just the array that you bound to the list, right? I might have to look at my own code tonight and think about what it might be.

    For the random numbers the following should give you a number between 1 and 10

    Random randomgenerator = new Random();
    randomgenerator.nextInt(9) +1;
     
    Beanstalk likes this.
  4. Beanstalk

    Beanstalk Member This Topic's Starter

    Joined:
    Jun 24, 2010
    Messages:
    9
    Likes Received:
    0
    Thanks a lot, that's 2/3 problems fixed :D
    Man, people on this forum are so awesome.

    @jonbonazza - The text file is stored in my "assets" folder. So what would my filepath look like?
     
  5. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    I have never really fooled around with assets, but I believe it would be something like this:

    Code (Text):
    1. public String readFile()
    2. {
    3.    
    4.     BufferedReader input;
    5.    
    6.         try
    7.         {
    8.             input = new BufferedReader(new FileReader(Activity.getAssets().open(assetName)));
    9.             String line;
    10.             line = input.readLine();
    11.             input.close();
    12.         }
    13.         catch(IOException e)
    14.         {
    15.             //handle exceptions here
    16.         }
    17.        
    18.     return line;
    19. }
     
  6. Beanstalk

    Beanstalk Member This Topic's Starter

    Joined:
    Jun 24, 2010
    Messages:
    9
    Likes Received:
    0
    So if I have an image stored in my res/drawable folder, I can reference it using R.drawable.<imagename>

    If I have a text file stored in my assets folder, how should I reference it to call the aforementioned constructor of the FileReader class?

    input = new BufferedReader(new FileReader(Activity.getAssets().open(assetName)));
     
  7. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    the variable assetName should be a string containing the name of the asset. I am not sure whether the string should include the file extension or not. Trial and error is your only really option at this point unless someone else chimes in.
     

Share This Page

Loading...