• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Grid View and Text File

Beanstalk

Lurker
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 =]
 
If the text file is on your sdcard, you might have something like this:

Code:
public String readFile()
{
	File txtFile = new File(filePath);
	BufferedReader input;
	
		try
		{
			input = new BufferedReader(new FileReader(txtFile));
			String line;
			line = input.readLine();
			input.close();
		}
		catch(IOException e)
		{
			//handle exceptions here
		}
		
	return line;
}

That will read a single line of code. You could put that into a loop to read multiple lines.
 
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;
 
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?
 
I have never really fooled around with assets, but I believe it would be something like this:

Code:
public String readFile()
{
	
	BufferedReader input;
	
		try
		{
			input = new BufferedReader(new FileReader(Activity.getAssets().open(assetName)));
			String line;
			line = input.readLine();
			input.close();
		}
		catch(IOException e)
		{
			//handle exceptions here
		}
		
	return line;
}
 
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)));
 
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)));

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.
 
Back
Top Bottom