-2

Help, I have this switch statement inside my code, but I want to include an if statement, but its not working.

   if (valueSumIndex > 10000) {
       Toast.makeText(getActivity(), "You are Over Spending", 
        Toast.LENGTH_SHORT).show();
         }
@Override
   public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
     switch (loader.getId()) {
         case SUM_LOADER_ID:
             int valueSumIndex = data.getColumnIndex(Expenses.VALUES_SUM);
             data.moveToFirst();
             float valueSum = data.getFloat(valueSumIndex);
             mTotalValueTextView.setText(Utils.formatToCurrency(valueSum));

             if (valueSum > 10000) {
                 Toast.makeText(getActivity(), "You are Over Spending", 
  Toast.LENGTH_SHORT).show();
             }
             break;

         case LIST_LOADER_ID:
             // Hide the progress bar
             mProgressBar.setVisibility(View.GONE);
             // Update adapter's data
             mAdapter.swapCursor(data);
             break;
     }

What I want is a notification instead of a toast saying you are overspending after spending more that 10000.

  • 2
    Looks reasonable, but I suspect you meant `if (valueSum > 10000)` because why would the **index** of that sum matter? – Elliott Frisch May 11 '19 at 12:13
  • How can I turn the toast to a notification instead sir – Folalu Timothy May 11 '19 at 13:46
  • https://stackoverflow.com/questions/7442670/android-how-to-show-notification-on-screen. Check this link. If you are looking for, how to display the notification on the screen, then the above link will helpful. – Madhu May 13 '19 at 09:44

2 Answers2

0

I tried something like you used and it worked nice.

public static void main(String[] args) {
    switch (2) {
        case 1: //do something
        case 2:
            int a = 2 + 1;
            if (a > 1) {
                System.out.println("It's works!");
            }
    }
}

It looks like that switch-statement don't exclude using if-statement inside.

0

you can do this way:-

@Override
   public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

       if(loader.getId() == R.id.yourId){
           //your condition
         }else if(loader.getId() == R.id.yourId){
           //your condition
         }
}
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17