• 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
Jun 24, 2010
9
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 =]
 
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.
 
  • Like
Reactions: Beanstalk
Upvote 0
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;
 
  • Like
Reactions: Beanstalk
Upvote 0
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;
}
 
Upvote 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)));
 
Upvote 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)));

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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones