1

Parece que tengo un conflicto entre PreferenceFragment y onOptionsItemSelected , estoy tratando de enviar varias variables de ListPreference desde UsbSettingActivity al MainActivity usando Intents, el problema esta en solo me llega el primer dato, usando el debug del SDK en este activity observó que solo está disponible el dato:

mconfigbaudrate = 9600

Aqui esta mi codigo

public class UsbSettingActivity extends AppCompatPreferenceActivity {

final String setBaudRate = "" ;
public static String mconfigbaudrate = "";
public static String mconfigdatabit = "";
public static String mconfigbitstop = "";
public static String mconfigparity = "";
public static String mconfigfontsize = "";
public static String mconfigconsolamode = "";
public static String mconfigfindelinea = "";
public static String mconfigenableeco = "";
public static String mconfigfilesave = "";

private static Preference.OnPreferenceChangeListener
        sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue( stringValue );
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null );
        }else {
            preference.setSummary( stringValue );
        }
        return true;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    setupActionBar();
}


private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
         actionBar.setDisplayHomeAsUpEnabled( true );
    }
}

private static void bindPreferenceSummaryToValue(Preference preference) {

    preference.setOnPreferenceChangeListener( sBindPreferenceSummaryToValueListener );
    sBindPreferenceSummaryToValueListener.onPreferenceChange( preference,PreferenceManager
            .getDefaultSharedPreferences( preference.getContext() )
            .getString( preference.getKey(), "" ) );
}

public static class MyPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_setup_com);

        setHasOptionsMenu( true );

        bindPreferenceSummaryToValue( findPreference( "config_baud_rate" ) );
        bindPreferenceSummaryToValue( findPreference( "config_data_bit" ) );
        bindPreferenceSummaryToValue( findPreference( "config_bit_stop" ) );
        bindPreferenceSummaryToValue( findPreference( "config_parity" ) );
        bindPreferenceSummaryToValue( findPreference( "config_flow_control" ) );
        bindPreferenceSummaryToValue( findPreference( "config_font_size" ) );
        bindPreferenceSummaryToValue( findPreference( "config_consola_mode" ) );
        bindPreferenceSummaryToValue( findPreference( "config_fin_delinea" ) );
        bindPreferenceSummaryToValue( findPreference( "config_filesave" ) );

        ListPreference SPconfigbaudrate2 = (ListPreference) findPreference("config_baud_rate");
        mconfigbaudrate = SPconfigbaudrate2.getValue();

        ListPreference SPconfigdatabit2 = (ListPreference) findPreference("config_data_bit");
        String mconfigdatabit = SPconfigdatabit2.getValue();

        ListPreference SPconfigbitstop2 = (ListPreference) findPreference("config_bit_stop");
        String mconfigbitstop = SPconfigbitstop2.getValue();

        ListPreference SPmconfigparity2 = (ListPreference) findPreference("config_parity");
        String mconfigparity = SPmconfigparity2.getValue();

        ListPreference SPconfigfontsize2 = (ListPreference) findPreference("config_font_size");
        String mconfigfontsize = SPconfigfontsize2.getValue();

        ListPreference SPconfigconsolamode2 = (ListPreference) findPreference("config_consola_mode");
        String mconfigconsolamode = SPconfigconsolamode2.getValue();

        ListPreference SPconfigfindelinea2 = (ListPreference) findPreference("config_fin_delinea");
        String mconfigfindelinea = SPconfigfindelinea2.getValue();

        TwoStatePreference SPconfigenableeco2 = (TwoStatePreference) findPreference("config_enable_eco");
        Boolean mconfigenableecoX = SPconfigenableeco2.isChecked();
        String mconfigenableeco = String.valueOf(mconfigenableecoX);

        ListPreference SPconfigfilesave2 = (ListPreference) findPreference("config_filesave");
        String mconfigfilesave = SPconfigfilesave2.getValue();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == android.R.id.home) {
        Intent intent2 = new Intent(this, MainActivity.class);
        intent2.putExtra("mconfigbaudratex",mconfigbaudrate);
        intent2.putExtra("mconfigdatabitx",mconfigdatabit);
        intent2.putExtra("mconfigbitstopx", mconfigbitstop);
        intent2.putExtra("mconfigparityx", mconfigparity);
        intent2.putExtra("mconfigfontsizex", mconfigfontsize);
        intent2.putExtra("mconfigconsolamodex", mconfigconsolamode);
        intent2.putExtra("mconfigfindelineax", mconfigfindelinea);
        intent2.putExtra("mconfigenableecox", mconfigenableeco);
        intent2.putExtra("mconfigfilesavex", mconfigfilesave);

        startActivityForResult(intent2, 0);
    }
    return super.onOptionsItemSelected( item );
   }
 }

Alguien me podria decir cual es mi error?, los datos en listPreference están cargados y se muestran correctamente, si reemplazo las demás variables a excepción del primero por cadena de texto todos me llegan correctamente

introducir la descripción de la imagen aquí

1 respuesta 1

1

El problema es que estas definiendo los mismos nombres de variables dentro del método onCreate() por lo tanto únicamente tendrán valor dentro de este método.

Usa las variables que defines en la clase:

public class UsbSettingActivity extends AppCompatPreferenceActivity {

final String setBaudRate = "" ;
public static String mconfigbaudrate = "";
public static String mconfigdatabit = "";
public static String mconfigbitstop = "";
public static String mconfigparity = "";
public static String mconfigfontsize = "";
public static String mconfigconsolamode = "";
public static String mconfigfindelinea = "";
public static String mconfigenableeco = "";
public static String mconfigfilesave = "";
...
...

Realiza el cambio para que las variables puedas ser enviadas con su respectivo valor vía intent :

 public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_setup_com);

        setHasOptionsMenu( true );

        bindPreferenceSummaryToValue( findPreference( "config_baud_rate" ) );
        bindPreferenceSummaryToValue( findPreference( "config_data_bit" ) );
        bindPreferenceSummaryToValue( findPreference( "config_bit_stop" ) );
        bindPreferenceSummaryToValue( findPreference( "config_parity" ) );
        bindPreferenceSummaryToValue( findPreference( "config_flow_control" ) );
        bindPreferenceSummaryToValue( findPreference( "config_font_size" ) );
        bindPreferenceSummaryToValue( findPreference( "config_consola_mode" ) );
        bindPreferenceSummaryToValue( findPreference( "config_fin_delinea" ) );
        bindPreferenceSummaryToValue( findPreference( "config_filesave" ) );

        ListPreference SPconfigbaudrate2 = (ListPreference) findPreference("config_baud_rate");
        mconfigbaudrate = SPconfigbaudrate2.getValue();

        ListPreference SPconfigdatabit2 = (ListPreference) findPreference("config_data_bit");
        //String mconfigdatabit = SPconfigdatabit2.getValue();                      mconfigdatabit = SPconfigdatabit2.getValue();

        ListPreference SPconfigbitstop2 = (ListPreference) findPreference("config_bit_stop");
        //String mconfigbitstop = SPconfigbitstop2.getValue();
        mconfigbitstop = SPconfigbitstop2.getValue();

        ListPreference SPmconfigparity2 = (ListPreference) findPreference("config_parity");
        //String mconfigparity = SPmconfigparity2.getValue();                    
         mconfigparity = SPmconfigparity2.getValue();

        ListPreference SPconfigfontsize2 = (ListPreference) findPreference("config_font_size");
        //String mconfigfontsize = SPconfigfontsize2.getValue();            
         mconfigfontsize = SPconfigfontsize2.getValue();

        ListPreference SPconfigconsolamode2 = (ListPreference) findPreference("config_consola_mode");
        //String mconfigconsolamode = SPconfigconsolamode2.getValue();        
        mconfigconsolamode = SPconfigconsolamode2.getValue();

        ListPreference SPconfigfindelinea2 = (ListPreference) findPreference("config_fin_delinea");
        //String mconfigfindelinea = SPconfigfindelinea2.getValue();
        mconfigfindelinea = SPconfigfindelinea2.getValue();

        TwoStatePreference SPconfigenableeco2 = (TwoStatePreference) findPreference("config_enable_eco");
        Boolean mconfigenableecoX = SPconfigenableeco2.isChecked();
        //String mconfigenableeco = String.valueOf(mconfigenableecoX);
        mconfigenableeco = String.valueOf(mconfigenableecoX);

        ListPreference SPconfigfilesave2 = (ListPreference) findPreference("config_filesave");
        //String mconfigfilesave = SPconfigfilesave2.getValue();   
        mconfigfilesave = SPconfigfilesave2.getValue();
    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.