Task: get a random value (text paragraph) from a file that we add to Android Studio. There are many paragraphs in the file. Received a random paragraph, sent it to the user.
What is better to use for this? SQLite, json, assets or something else?
Task: get a random value (text paragraph) from a file that we add to Android Studio. There are many paragraphs in the file. Received a random paragraph, sent it to the user.
What is better to use for this? SQLite, json, assets or something else?
If the text does not change, then the easiest and perhaps the most convenient option is the resources) you create a regular string resource, you can in a separate file not to turn the work with strings.xml into a wheel spin simulator (you can create arbitrary files in the values folder). The aapt line breaks put in the resource file will be eliminated successfully, but \ n will remain and will be processed correctly, you can successfully separate paragraphs with their help. If the text is not needed entirely - string-array will help
SQLite is an extremely inconvenient option. You will have to put the finished database into assets, from there in runtime copy to disk and open the database only from a file on disk
json still need to be stored somewhere, which deprives its use of meaning
Storing a text file in assets is a bit more convenient than a database, but you still have to open a stream, copy its contents to the buffer, and create a string from the buffer
If resources do not suit there is the last option - to register with static fields or a static array. But this option is not welcome, incl. and the android studio
It is not practical to use storage in the file of the entire text if you do not receive this file while the program is running (for example, from the Internet), since a substantial resource is required to separate paragraphs from the whole text. It is much more reasonable to prepare the structure at the stage of creating the application. It also does not make much sense to use a database, JSON, and the like, since this also requires additional work. Such a decision can be justified with significant amounts of text, when it is easier to assign the program to sort the text than it is to itself.
Since you need to work separately with paragraphs, the simplest solution to your question will be to create an array of strings, where each element of the array is one paragraph. So we can very simply select the desired paragraph by index in the array.
To do this, create a simple resource (let's call it paragraphs.xml and put it in the res / values / folder). The resource will contain the text of our paragraphs in the format string-array
for later conversion into an array of strings. Placing a resource in a file is more convenient because the code itself does not clog, and if necessary, you can very easily implement support for different languages by means of the system itself.
paragraphs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="paragraphs"> <item>paragraph 1</item> <item>paragraph 2</item> <item>paragraph 3</item> </string-array> </resources>
Next, a simple code that, when a button is TextView
displays a random paragraph in TextView
:
public class MainActivity extends AppCompatActivity { TextView textView; Button button; String [] paragraphs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); button = findViewById(R.id.button); // получаем абзацы в массив paragraphs = getResources().getStringArray(R.array.paragraphs); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // генерируем случайное число в диапазоне полученного массива и выводим на экран textView.setText(paragraphs[new Random().nextInt(paragraphs.length)]); } }); } }
The code will print in random order: paragraph 1, paragraph 2, paragraph 3
Source: https://ru.stackoverflow.com/questions/974241/
All Articles