Listview with config.properties

Asked

Viewed 46 times

1

I am with a following doubt, I am using a listview with about 300 characters of a game, when clicking on a character he inserts some dice that is written in the config.properties, all characters have descriptions, vocations and other different information.

The code is working properly, but my doubt is the way I’m entering the data will not make the code too heavy? There’s another way more correct?

Code:

Util.java.

    public class Util {
    public static String getProperty(String key, Context context) throws IOException {
        Properties properties = new Properties();;
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open("config.properties");
        properties.load(inputStream);
        return properties.getProperty(key);

    }
}

Ivysauractivity.java

     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ivysaur);
        //mToolbar = (Toolbar) findViewById(R.id.Toolname);
        mToolbar = (TextView) findViewById(R.id.Toolname);

 Number = (TextView) findViewById(R.id.Number);
        Level = (TextView) findViewById(R.id.Level);
        Valornpc = (TextView) findViewById(R.id.Valornpc);

  Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mToolbar.setText(bundle.getString("TituloPokemon"));

            if (mToolbar.getText().toString().equalsIgnoreCase("Bulbasaur")) {

                try {
                    Number.setText(Util.getProperty("Number",getApplicationContext()));
                    Level.setText(Util.getProperty("Level",getApplicationContext()));
                    Valornpc.setText(Util.getProperty("Valornpc",getApplicationContext()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else
            if (mToolbar.getText().toString().equalsIgnoreCase("Venusaur")) {

                try {
                    Number.setText(Util.getProperty("Number2",getApplicationContext()));
                    Level.setText(Util.getProperty("Level2",getApplicationContext()));
                    Valornpc.setText(Util.getProperty("Valornpc2",getApplicationContext()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}

config.properties

Number=Teste1
Level=240
Valornpc=28.000;


Number2=Teste2
Level2=540
Valornpc2=58.000;

Thanks in advance

1 answer

2


I believe that this does not have a "correct" answer, it goes a lot of testing and how you want to create your database and the purpose of your application. For example:

  • If it is only to show Pokémon data and statistics. You can use a less complex approach. Maybe even as you are doing.
  • An offline game. Need to ensure possible changes in the properties of each character by malicious users.
  • A multiplayer game (online). A much more complex, secure and external approach is required. Servers, web services, authentications, etc.

Consider that these are just examples and that there are several factors involved. But here are some tips.

If you have a file .properties for each Pokemon and its evolutions, there I assure you that would not be the best option, because you would have an exorbitant amount of files. In that case, it would be better to leave all your Pokémon in one .properties;

You should take into account that this file may be altered by any case is saved locally (which may influence your application depending on the objective).


A safer alternative would be to create a bank SQLite local. It is the most "standard" android form for applications that need to save data locally, but one must take into account the size of the bank, not to occupy a very large space on the user’s device.

An example tutorial.


Another alternative, which is perhaps the most secure but more complex, is to create a database on a server that communicates through a web-service, where it communicates with your application. This leaves the application totally free to worry about storage, it only performs requests to the web-service which in turn returns the data. It’s much safer too.

Example android communication with server php and bank MySQL:

Exemplo Comunicação

  • Got it, how do I save . properties elsewhere but not locally?

  • You need a web server. You save the file to it and then access via HTTP or FTP from your source. Ex HTTP: URL url = new URL("http://seuservidor.com.br/config.properties"); But that’s a matter for another question.

  • 1

    Zulian, nice explanation. + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.