Select cities with Sharedpreference

Asked

Viewed 42 times

1

Only when I click on "Itaborai" that saves the preference, how do I select the other items in the list view among other activitys? As an example, the customer clicked on "Itaborai" when he opens the app again will open the Itaborai class directly. I want to put the "Itaborai" function in the other items of listview to open new classes.

 public class Selecionar_Cid extends ActionBarActivity {


ListView lista;

String[] config = new String[]{
        "Itaborai",
        "Cachoeiras",
        "Manilha",


};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selecionar);


    // Verfificar se a classe já foi selecionada
    int city = Integer.parseInt(Pref.retrievePrefKeyValue(getApplicationContext(), "city", "-1"));
    if (city != -1) {
        Intent i = new Intent(Selecionar_Cid.this, Verificar_Internet_Itaborai.class);
        startActivity(i);



    }


    lista = (ListView) findViewById(R.id.listView2);
    ArrayAdapter adaptador = new ArrayAdapter(this, android.R.layout.simple_list_item_1, config);

    lista.setAdapter(adaptador);
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView adapterView, View view, int posicion, long l) {
            Pref.savePrefKeyValue(getApplicationContext(), "city", String.valueOf(posicion));


            // TODO colocar o clique nas outras cidades

            switch (posicion) {


                case 0:
                    Intent i = new Intent(Selecionar_Cid.this, Verificar_Internet_Itaborai.class);
                    startActivity(i);
                    break;

                default:

            }
        }
    });
}


}

Preference

public class Pref {


public static void savePrefKeyValue( Context context, String key, String value ){
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor e = sp.edit();
    e.putString( key, value );
    e.apply();
}
public static String retrievePrefKeyValue( Context context, String key, String... defaultValue ){
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences( context );
    String dValue = defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : "";
    sp.getString(key, dValue );
    return( sp.getString( key, dValue ) );
}

}

No answers

Browser other questions tagged

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