How to store simple static predefined data in Android - java

Here is the thing; I have to store simple data which I have to define once (manually). I must have a functionality to search in it after using keywords. It's like this:
My first item
title:my_title
description:my_description (long, few hundreds words)
keyword1:my_keyword1
keywordx:my_keywordx
And I want a lot of items like this. For example 100 or 1000.
And after in code I want to make search function to look for specific items (as result may be a few, not only one) based on keywords and show the result as text in TextView field for example.
Do You have any idea how I should storage this data? I would prefer .xml file (person who will create data is not a programmer, it'll be much easier for him).

Put your data in JSON format as a text file, create a "res" folder, and put that text file in there. From the "your activity", read this file from the raw folder and parse the JSON.

Related

How to make Excel's "Text to columns" in Java?

I am trying to write something in Java that basically does the same thing as "Text-to-Columns" in Excel. I have a long string of data (parsed out from some HTML) and I formatted it to divide the data I need by a semicolon.
For example
Str = brand name;1242141;brand name;753216;brand name;2356123;brand name;656258;
So I want each of the brand names and numbers to be in their own cell. Any ideas on how to do this?
http://www.tutorialspoint.com/java/java_string_split.htm
Str.Split(";")
Should give you the information that you would like. Getting the information into Excel is a whole other bag of cats, but
Best way to export data from Java to MS Excel
Is probably a great way to start.

Fast search of proper XML file in folder

If I have 100 short XML files in folder and I'd like to know which of them contains text aaabbbccc (for later accurate parsing). Is this a good idea to read them as Strings one after other and try to use contains function to determine what files not contains this text?
As I know the contains function is very fast.

Android Intents and Lists

I plan on reading several files when my app/game is created and using the information from them for the entirety of the app. I also have to write to the file at one point.
I have two files. One is a 2-column text file that I'll turn into a dictionary for fast searching. The other is a text file that has 11 columns. I'll make a dictionary out of two of the columns, and the other data I need kept as is so I can write to the columns to count the amount of times something happens in different circumstances for datamining.
Currently, I've turned the second file into a list of a list of strings, or List>. I can't figure out how to pass that around in intents. ".putStringArrayListExtra" only works for a list of strings.
Am I going about this the wrong way entirely? This is my first real Android app.
In order to store a data structure into an Intent, it has to be either serializable or parcelable. If your data structure is neither of them, you might create a class that would implement Serializable and manage it. A good example might be found here.
Once done, you then might use Intent.putSerializable(...) to store your data structure. See this:
Using putSerializable in Android
Additionally to this, if you could convert your structure into a JSON structure, you'd already have it done since it would be treated as a String. If not, the above solution should be easy to do.

how to make table formatted text file in android sd card?

I see this to make text file and it also helps me out but in all examples i see that they just making string in notepad or we can say text file...
Can any one say that how to make table formatted text file in android??
i want to make file(invoice)
This is most likely going to involve some some slightly messy string processing. Assuming you have your data in an acceptable format (such as string arrays), you should be able to construct a single java string representing the whole table, and then use the code you found already to print it to a file. Use the escape character \t to separate between columns and \n to separate between rows.
That would be TSV format, and it is very easy to generate. Just add a TAB after every field, and a CR/LF pair after every record.

implement Bookmark list in java

I am developing a program which has three JTextBox which my users can enter and check some text for right rule.
So I want add a ablitiy to my program that my users can add or remove their favorite text to a Favorite List and can create folder in Favorite list and put some text in it, such as Bookmark library in FireFox or other web browser.
I want use RandomAccessFile to save favorite list as a favorite source.
How do I implemet it? is there beter way to implement it? is there beter way from RandomAccessFile?
Can any one help me?
Thanks.
There could be lots of approaches. It all depends on what you want to achieve.
Consider using Java serialization mechanism. You can serialize a collection of bookmarks to a file. When your app starts, you deserialize it, and get the same collection data.
The advantages are: simple and easy implementation. The disadvantages: you can't look through stored bookmarks in a text editor or something. The same class hierarchy is to be used to load the serialized version.
XML is human-readable and provides easy interoperability. Other applications would be able to handle your list of bookmarks.
It usually takes more resources to parse the XML and load it to memory and then to create the internal object structures. Though you can use the DOM to traverse the tree all the time, it could be not as convenient as the internal data structure using specialized classes.
Random Access Files work best with fixed record sizes. It means all the fields of your bookmarks must be fixed-length. For example, the name of a bookmark is String. When you write it out to a file, you store it like an array of a fixed length, let's say 20. This automatically implies that if users give a bookmark the name which length is greater than 20, the remaining characters would be lost.
It is also easy to implement with the caveats above. Of course the records could be of variable length, but then you lose the random access to file because you cannot easily calculate the position of a specific record.
Firefox uses JSON for storing bookmarks and allows exporting to HTML. You can explore this too.
You can also store bookmarks, and things you want to keep between sessions in the Preferences,
see http://download.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html

Categories

Resources