View Full Version : [SOLVED] Android: Displaying content?

October 28th, 2011, 06:31 PM
If I want to display content like alot of Strings what would be the recommended method?

1. An array that exports to a text file and imports the data back in.

2. A database of some sort.

October 28th, 2011, 08:13 PM
When you say "a lot" please be a bit more specific. Both of those choices are feasible but require different amounts of work. A database would be better if you need to perform operations like searching etc. but if you are just storing and reading sequentially, a text file might be easier.

Also, how would you like to display these strings? in a table? a scrolling list?

October 28th, 2011, 08:28 PM
I won't need to search the data... so maybe an array or variables would be good and then store to a text file. Im not sure yet how I want to display the data... I suppose a table would be good or whatever is easiest. :)

October 28th, 2011, 08:37 PM
Do you mind if I ask what you intend to do with this app?

Is saving the text for the future necessary, or are you just concerned with memory usage?

What kind of text is it? In what form would it look most natural?

October 31st, 2011, 01:14 PM
Im developing this app just for learning Android development. It won't serve any purpose but I really want to know how to store data and restore it like in desktop apps.

The text will be strings and numbers parsed to strings.. not sure what would suit it best.

However Im having another problem.. ive been trying to get a Linearview to be scrollable but the emulator force closes the app when it loads the activity.

Anything wrong with this?:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView ...>

<LinearLayout ...>




October 31st, 2011, 02:37 PM
That layout looks ok to me. is there anything else in the program that could be making it crash?

As far as reading and writing text to/from the SD card you should try something like the following:


File sd_dir = Environment.getExternalStorageDirectory();

File input = new File(sd_dir,"input.txt");

String text = "";

try {
BufferedReader reader = new BufferedReader(new FileReader(input));
String line_in;

while ((line_in = reader.readLine()) != null) {
text = text + line_in + "\n";
catch (IOException e) {


//add text to your TextView

Writing will follow the same general pattern, but will use a FileOutputStream class

November 2nd, 2011, 04:58 PM
Fixed the crashing! :)

Thanks for that code, awesome!

Having major trouble trying to use spinners though. (comboboxes) I copied example code from a book and it doesn't work! :D

Do you know how to use them? or know of an example online?

November 2nd, 2011, 05:05 PM
What exactly isn't working with the spinner?

The use described on the android tutorials always worked for me.


November 2nd, 2011, 06:33 PM
Thanks! I had trouble putting items into the spinner, I guess maybe I was working with a bad example or something. Official documentation is best! :)

November 16th, 2011, 06:12 PM
Another question... how does one setup onclicklisteners for alertdialogs??

I have an alertdialog using a CharSequence and I want to setup actions for each option however I can't seem to figure it out.

November 17th, 2011, 02:02 PM
I'm going to point you back to the official documentation.


EDITED: Just looked through some of my old code, the methods described in the documentation still work. Sorry if that misled you.

November 17th, 2011, 03:38 PM
Ah I figured it out now... im having another problem though.

All I want to do is pass an int to another activity but nothing on the internet works.

At the moment I have:

Sending the value:

int myVariable = 1;

Bundle b=new Bundle();
b.putInt("key", myVariable);

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

Receiving the value:

Bundle b=this.getIntent().getExtras();
int s=b.getInt("Key");

Any ideas as to why this doesn't work?

November 17th, 2011, 03:49 PM
Is it because "key" is capitalized in activity 2? Thats the only problem I can see with what you showed me.

November 17th, 2011, 07:26 PM
I fixed that but it still force closes when I test the code. :lolflag:

November 17th, 2011, 11:01 PM
I don't think there is anything else wrong with what you posted. Can you post more code? It may be choking on something else.

November 19th, 2011, 01:32 PM
Here you go:


Its very small so far... just the beginning of something.

I recommend opening with Eclipse setup with Android if you want to test it.

November 21st, 2011, 03:04 PM
Can someone show me an example of variable passing?

November 21st, 2011, 05:01 PM
You are on the right track using bundles to pass between activities, and from looking at your code, it looks like you are using them correctly. Are you sure that your program is choking on the bundle?

In other words, try commenting out the bundle calls and just put a Toast.makeText in the GameActivity onCreate to make sure that everything else is working. I can't see anything wrong from looking, but I don't want to debug your code for you.

I do recommend using LogCat, though, if you are not already. It is very helpful for debugging. You can set some breakpoints and step through the code while looking at logcat for warning/error messages

November 21st, 2011, 07:33 PM
Hmm... even a really, really simple app to switch activity force closes on me. Tested it on Win7 and Ubuntu... something very strange is going on. :confused:

November 21st, 2011, 07:44 PM
That should not be a problem with your operating system. The answer may be very obvious to you by looking at logcat error messages

November 22nd, 2011, 05:02 PM
Silly me, I forgot to edit the android manifest file! The code for variable switching now works! :lolflag: