Reputation: 1112
After extensively trying to figure out why my SharedPreferences are always blank, or never created. I have concluded that it is because the edit text to string isn't doing anything, yet I have no idea why it isn't doing anything.
Here is the code
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class Friday extends Fragment{
private String GBand, BBand, ADV1Band, ADV2Band, CBand, FBand;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.friday, container, false);
TextView GValue = (TextView) view.findViewById(R.id.bandText111111);
GValue.setText( "G Band" );
EditText GEdit = (EditText) view.findViewById(R.id.editText111111);
GBand = GEdit.getText().toString();
TextView BValue = (TextView) view.findViewById(R.id.bandText222222);
BValue.setText( "B Band" );
EditText BEdit = (EditText) view.findViewById(R.id.editText222222);
BBand = BEdit.getText().toString();
TextView ADV1Value = (TextView) view.findViewById(R.id.bandText333333);
ADV1Value.setText( "Advisory 1" );
EditText ADV1Edit = (EditText) view.findViewById(R.id.editText333333);
ADV1Band = ADV1Edit.getText().toString();
TextView ADV2Value = (TextView) view.findViewById(R.id.bandText444444);
ADV2Value.setText( "Advisory 2" );
EditText ADV2Edit = (EditText) view.findViewById(R.id.editText444444);
ADV2Band = ADV2Edit.getText().toString();
TextView CValue = (TextView) view.findViewById(R.id.bandText555555);
CValue.setText( "C Band" );
EditText CEdit = (EditText) view.findViewById(R.id.editText555555);
CBand = CEdit.getText().toString();
TextView FValue = (TextView) view.findViewById(R.id.bandText666666);
FValue.setText( "F Band" );
EditText FEdit = (EditText) view.findViewById(R.id.editText666666);
FBand = FEdit.getText().toString();
return view;
}
public class EditTextWatcher implements TextWatcher {
private final TextView target;
private EditTextWatcher(TextView target) {
this.target = target;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
this.target.setText(s);
}
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.android_apply, menu);
}
}
public boolean onOptionsItemSelected(android.view.MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.apply:
SharedPreferences sharedPref = getActivity().getSharedPreferences("schedule",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("g_Friday", GBand);
editor.putString("b_Friday", BBand);
editor.putString("adv1_Friday", ADV1Band);
editor.putString("adv2_Friday", ADV2Band);
editor.putString("c_Friday", CBand);
editor.putString("f_Friday", FBand);
editor.commit();
Intent in = new Intent(getActivity(), MainActivity.class);
startActivity(in);
return true;
}
return true;
}
}
Upvotes: 0
Views: 751
Reputation: 1036
Try making the edittexts protected and adding the line "string" = "edittext".getText().toString(); inside afterTextChanged()
Upvotes: 2
Reputation: 14274
Take a close look at what you are doing there:
EditText
when you create the view. By default, the EditText
s should be empty then.EditText
s -- you are simply using the empty values you retrieved in the beginning.Side note: Java coding conventions usually call for lower-case identifiers for variables, and upper-case identifiers only for classes, interfaces, etc. To a seasoned Java developer, your code will be somewhat unreadable.
To fix it, follow this pattern (shown only for GBand):
private String...
members and declare private EditText edtGBand;
onCreateView()
, include edtGBand = (EditText) view.findViewById( R.id.editText1111111 );
onOptionsItemSelected
, use editor.putString( "g_Friday", edtGBand.getText().toString() );
Upvotes: 4