I have stored the spinner item on the Sqlite db successfully but when i reopen my app the spinner object...












0















I am working on saving the spinner object on my SQLite database, I have managed to get it to display on my adapter but everytime I close my app and reopen it says null but when I navigate to another space and go back it restores, below is my code what am I doing wrong?



public class ExpenseDetailFragment extends Fragment  {
private View view;
private ExpenseData selectedExpenseData;
private final int PICKER_REQUEST = 1;
private Activity activity = null;

private String currency = "USD";
private Spinner currencyd;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_expense_detail, container, false);
activity = this.getActivity();

selectedExpenseData = (ExpenseData) getArguments().getSerializable("selectedExpenseData");
ImageButton storeExpenseButton = getActivity().findViewById(R.id.addExpense);
Button deleteButton = view.findViewById(R.id.deleteButton);

currencyd = getActivity().findViewById(R.id.currency_spinner);

storeExpenseButton.setImageResource(R.drawable.ic_action_done);
currencyd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


currency = adapterView.getItemAtPosition(i).toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

//adapterView.getLastVisiblePosition();

}
});


//button
storeExpenseButton.setOnClickListener(v -> {


String cur = currencyd.getSelectedItem().toString();


TextView amountTextView1 = getActivity().findViewById(R.id.amount);
try {

double amount = Double.valueOf(amountTextView1.getText().toString());

selectedExpenseData.setExpenseAmount(amount);
} catch (NumberFormatException e) {

Toast.makeText(getContext(), "Budget created", Toast.LENGTH_SHORT).show();
}

selectedExpenseData.setCurrency(cur);
if (selectedExpenseData.getId() != 0)
updateExpense(v, selectedExpenseData);
else
storeExpense(v, selectedExpenseData);
});




return view;
}


private void storeExpense(View view, ExpenseData expenseData) {


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());


if (uri == null) {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_failed,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_successful,
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix

}

private void updateExpense(@SuppressWarnings("unused") View view, ExpenseData expenseData) {
Uri currentExpenseURI = ContentUris.withAppendedId(ExpensesContract.ExpenseEntry.CONTENT_URI, expenseData.getId());


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());

int rowsAffected = getActivity().getContentResolver().update(currentExpenseURI, values, null, null);

if (rowsAffected == 0) {
Toast.makeText(this.getActivity(), getString(R.string.editor_update_expense_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getContext(), getString(R.string.editor_update_expense_successful),
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix
}









share|improve this question























  • you should retrieve data from database when you open app

    – John Joe
    Nov 21 '18 at 3:02













  • How? I still don't all other instances are saved fine, just the spinner.

    – Anna Murray
    Nov 21 '18 at 3:05











  • did spinner value saved to SQLite successfully ?

    – John Joe
    Nov 21 '18 at 3:10
















0















I am working on saving the spinner object on my SQLite database, I have managed to get it to display on my adapter but everytime I close my app and reopen it says null but when I navigate to another space and go back it restores, below is my code what am I doing wrong?



public class ExpenseDetailFragment extends Fragment  {
private View view;
private ExpenseData selectedExpenseData;
private final int PICKER_REQUEST = 1;
private Activity activity = null;

private String currency = "USD";
private Spinner currencyd;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_expense_detail, container, false);
activity = this.getActivity();

selectedExpenseData = (ExpenseData) getArguments().getSerializable("selectedExpenseData");
ImageButton storeExpenseButton = getActivity().findViewById(R.id.addExpense);
Button deleteButton = view.findViewById(R.id.deleteButton);

currencyd = getActivity().findViewById(R.id.currency_spinner);

storeExpenseButton.setImageResource(R.drawable.ic_action_done);
currencyd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


currency = adapterView.getItemAtPosition(i).toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

//adapterView.getLastVisiblePosition();

}
});


//button
storeExpenseButton.setOnClickListener(v -> {


String cur = currencyd.getSelectedItem().toString();


TextView amountTextView1 = getActivity().findViewById(R.id.amount);
try {

double amount = Double.valueOf(amountTextView1.getText().toString());

selectedExpenseData.setExpenseAmount(amount);
} catch (NumberFormatException e) {

Toast.makeText(getContext(), "Budget created", Toast.LENGTH_SHORT).show();
}

selectedExpenseData.setCurrency(cur);
if (selectedExpenseData.getId() != 0)
updateExpense(v, selectedExpenseData);
else
storeExpense(v, selectedExpenseData);
});




return view;
}


private void storeExpense(View view, ExpenseData expenseData) {


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());


if (uri == null) {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_failed,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_successful,
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix

}

private void updateExpense(@SuppressWarnings("unused") View view, ExpenseData expenseData) {
Uri currentExpenseURI = ContentUris.withAppendedId(ExpensesContract.ExpenseEntry.CONTENT_URI, expenseData.getId());


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());

int rowsAffected = getActivity().getContentResolver().update(currentExpenseURI, values, null, null);

if (rowsAffected == 0) {
Toast.makeText(this.getActivity(), getString(R.string.editor_update_expense_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getContext(), getString(R.string.editor_update_expense_successful),
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix
}









share|improve this question























  • you should retrieve data from database when you open app

    – John Joe
    Nov 21 '18 at 3:02













  • How? I still don't all other instances are saved fine, just the spinner.

    – Anna Murray
    Nov 21 '18 at 3:05











  • did spinner value saved to SQLite successfully ?

    – John Joe
    Nov 21 '18 at 3:10














0












0








0








I am working on saving the spinner object on my SQLite database, I have managed to get it to display on my adapter but everytime I close my app and reopen it says null but when I navigate to another space and go back it restores, below is my code what am I doing wrong?



public class ExpenseDetailFragment extends Fragment  {
private View view;
private ExpenseData selectedExpenseData;
private final int PICKER_REQUEST = 1;
private Activity activity = null;

private String currency = "USD";
private Spinner currencyd;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_expense_detail, container, false);
activity = this.getActivity();

selectedExpenseData = (ExpenseData) getArguments().getSerializable("selectedExpenseData");
ImageButton storeExpenseButton = getActivity().findViewById(R.id.addExpense);
Button deleteButton = view.findViewById(R.id.deleteButton);

currencyd = getActivity().findViewById(R.id.currency_spinner);

storeExpenseButton.setImageResource(R.drawable.ic_action_done);
currencyd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


currency = adapterView.getItemAtPosition(i).toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

//adapterView.getLastVisiblePosition();

}
});


//button
storeExpenseButton.setOnClickListener(v -> {


String cur = currencyd.getSelectedItem().toString();


TextView amountTextView1 = getActivity().findViewById(R.id.amount);
try {

double amount = Double.valueOf(amountTextView1.getText().toString());

selectedExpenseData.setExpenseAmount(amount);
} catch (NumberFormatException e) {

Toast.makeText(getContext(), "Budget created", Toast.LENGTH_SHORT).show();
}

selectedExpenseData.setCurrency(cur);
if (selectedExpenseData.getId() != 0)
updateExpense(v, selectedExpenseData);
else
storeExpense(v, selectedExpenseData);
});




return view;
}


private void storeExpense(View view, ExpenseData expenseData) {


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());


if (uri == null) {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_failed,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_successful,
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix

}

private void updateExpense(@SuppressWarnings("unused") View view, ExpenseData expenseData) {
Uri currentExpenseURI = ContentUris.withAppendedId(ExpensesContract.ExpenseEntry.CONTENT_URI, expenseData.getId());


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());

int rowsAffected = getActivity().getContentResolver().update(currentExpenseURI, values, null, null);

if (rowsAffected == 0) {
Toast.makeText(this.getActivity(), getString(R.string.editor_update_expense_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getContext(), getString(R.string.editor_update_expense_successful),
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix
}









share|improve this question














I am working on saving the spinner object on my SQLite database, I have managed to get it to display on my adapter but everytime I close my app and reopen it says null but when I navigate to another space and go back it restores, below is my code what am I doing wrong?



public class ExpenseDetailFragment extends Fragment  {
private View view;
private ExpenseData selectedExpenseData;
private final int PICKER_REQUEST = 1;
private Activity activity = null;

private String currency = "USD";
private Spinner currencyd;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_expense_detail, container, false);
activity = this.getActivity();

selectedExpenseData = (ExpenseData) getArguments().getSerializable("selectedExpenseData");
ImageButton storeExpenseButton = getActivity().findViewById(R.id.addExpense);
Button deleteButton = view.findViewById(R.id.deleteButton);

currencyd = getActivity().findViewById(R.id.currency_spinner);

storeExpenseButton.setImageResource(R.drawable.ic_action_done);
currencyd.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


currency = adapterView.getItemAtPosition(i).toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

//adapterView.getLastVisiblePosition();

}
});


//button
storeExpenseButton.setOnClickListener(v -> {


String cur = currencyd.getSelectedItem().toString();


TextView amountTextView1 = getActivity().findViewById(R.id.amount);
try {

double amount = Double.valueOf(amountTextView1.getText().toString());

selectedExpenseData.setExpenseAmount(amount);
} catch (NumberFormatException e) {

Toast.makeText(getContext(), "Budget created", Toast.LENGTH_SHORT).show();
}

selectedExpenseData.setCurrency(cur);
if (selectedExpenseData.getId() != 0)
updateExpense(v, selectedExpenseData);
else
storeExpense(v, selectedExpenseData);
});




return view;
}


private void storeExpense(View view, ExpenseData expenseData) {


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());


if (uri == null) {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_failed,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getActivity(), R.string.editor_save_expense_successful,
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix

}

private void updateExpense(@SuppressWarnings("unused") View view, ExpenseData expenseData) {
Uri currentExpenseURI = ContentUris.withAppendedId(ExpensesContract.ExpenseEntry.CONTENT_URI, expenseData.getId());


ContentValues values = new ContentValues();

values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY, expenseData.getCurrency());

int rowsAffected = getActivity().getContentResolver().update(currentExpenseURI, values, null, null);

if (rowsAffected == 0) {
Toast.makeText(this.getActivity(), getString(R.string.editor_update_expense_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this.getContext(), getString(R.string.editor_update_expense_successful),
Toast.LENGTH_SHORT).show();
checkBudgets(expenseData.getExpenseName());
}
getActivity().finish();

Intent intent = new Intent(this.getActivity(), MainActivity.class); //remove this after fix
startActivity(intent);//remove this after fix
}






java android android-spinner






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 2:36









Anna MurrayAnna Murray

398




398













  • you should retrieve data from database when you open app

    – John Joe
    Nov 21 '18 at 3:02













  • How? I still don't all other instances are saved fine, just the spinner.

    – Anna Murray
    Nov 21 '18 at 3:05











  • did spinner value saved to SQLite successfully ?

    – John Joe
    Nov 21 '18 at 3:10



















  • you should retrieve data from database when you open app

    – John Joe
    Nov 21 '18 at 3:02













  • How? I still don't all other instances are saved fine, just the spinner.

    – Anna Murray
    Nov 21 '18 at 3:05











  • did spinner value saved to SQLite successfully ?

    – John Joe
    Nov 21 '18 at 3:10

















you should retrieve data from database when you open app

– John Joe
Nov 21 '18 at 3:02







you should retrieve data from database when you open app

– John Joe
Nov 21 '18 at 3:02















How? I still don't all other instances are saved fine, just the spinner.

– Anna Murray
Nov 21 '18 at 3:05





How? I still don't all other instances are saved fine, just the spinner.

– Anna Murray
Nov 21 '18 at 3:05













did spinner value saved to SQLite successfully ?

– John Joe
Nov 21 '18 at 3:10





did spinner value saved to SQLite successfully ?

– John Joe
Nov 21 '18 at 3:10












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404553%2fi-have-stored-the-spinner-item-on-the-sqlite-db-successfully-but-when-i-reopen-m%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404553%2fi-have-stored-the-spinner-item-on-the-sqlite-db-successfully-but-when-i-reopen-m%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$