2017-03-24 6 views
0

私は、さまざまな支払いモードがあり、カード支払いと支払いの支払いを行うアプリを作成しています。私は、ユーザーから詳細を取得してデータを保存する2つの異なるアクティビティを作成しました共有の設定にしてから、アプリは他の詳細もそこにあるアクティビティに戻り、ユーザはボタンのクリックでデータを保存することができます。このデータはSqliteデータベースに保存されます。 私の問題は、私がカードの支払いを選択しているときに、適切に保存されているが、同じ値もチェックに保存されています。いいえ、sqlite database.Inshortには、カード支払いの値がデフォルトでチェックされません。以下共有環境設定の2つの異なるキーに対して同じ値を取得

カード決済活動のために私のコードです:

public class CardNo extends Activity { 

String bankname; 
String cardno; 
int chq; 
TextView textView1, textView2; 
EditText editText1, editText2; 
Button btn; 


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

    textView1 = (TextView) findViewById(R.id.tv1); 
    textView2 = (TextView) findViewById(R.id.tv2); 
    editText1 = (EditText) findViewById(R.id.bankname); 
    editText2 = (EditText) findViewById(R.id.cardno); 
    btn = (Button) findViewById(R.id.btn1); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      saveData(); 

      Intent card = new Intent(CardNo.this, EnterAmount.class); 
      startActivity(card); 
      finish(); 
     } 
    }); 

} 

private void saveData() { 

    bankname = editText1.getText().toString(); 
    cardno = editText2.getText().toString(); 


    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putString("Bank Name", bankname); 
    editor.putString("Card No", cardno); 
    editor.apply(); 


} 
} 

チェック支払活動のために今すぐコード:

 public class Cheque extends Activity { 
String bankname1; 
String chequeno; 
int chq; 
TextView textView1,textView2; 
EditText editText1,editText2; 
Button btn; 

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


    textView1=(TextView)findViewById(R.id.tv11); 
    textView2=(TextView)findViewById(R.id.tv12); 
    editText1=(EditText)findViewById(R.id.bankname1); 
    editText2=(EditText)findViewById(R.id.chequeno); 
    btn=(Button)findViewById(R.id.btn11); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      saveData(); 

      Intent cheque = new Intent(Cheque.this, EnterAmount.class); 

      startActivity(cheque); 
      finish(); 

     } 
    }); 

} 

private void saveData() { 

    bankname1 = editText1.getText().toString(); 
    chequeno = editText2.getText().toString(); 


    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putString("Bank Name", bankname1); 
    editor.putString("Cheque No", chequeno); 
    editor.apply(); 

} 
} 

私は共有設定からデータを取得しています活動のコードとデータをsqliteに格納します。

 public class EnterAmount extends Activity implements OnClickListener { 
    Intent intent; 
    Button save; 
    Spinner spinnerPayment, spinnerCategory; 
    EditText etamt, etbdgt, et_get_other; 
    String date, sBdgt, budget, bankname, cardno, chequeno; 
    String sAmt; 
    String spinnerItemSelectedPayment; 
    String spinnerItemSelectedCategory; 
    // String category; 
    int amt; 
    int date2; 
    TextView caategories, tv_cat; 


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

      save = (Button) findViewById(R.id.bsaveDb); 
      caategories = (TextView) findViewById(R.id.tvCaategories); 
      etamt = (EditText) findViewById(R.id.etAmount); 
      etbdgt = (EditText) findViewById(R.id.etbudget); 

      spinnerCategory = (Spinner) findViewById(R.id.spinnerCategory); 

      spinnerPayment = (Spinner) findViewById(R.id.payment_spinner); 

      List<String> sCategory = new ArrayList<String>(); 
      String[] categories = {"Food", "Bills", 
        "Travel", "Entertainment", "Office Stationary", 
        "Medical Expenses", "Fuel" 
      }; 

      sCategory.add("Food"); 
      sCategory.add("Office Stationary"); 
      sCategory.add("Bills"); 
      sCategory.add("Travel"); 
      sCategory.add("Entertainment"); 
      sCategory.add("Medical Expenses"); 
      sCategory.add("Fuel"); 

      ArrayAdapter<String> sc = new ArrayAdapter<String>(this, 
        android.R.layout.simple_spinner_dropdown_item, sCategory); 
      spinnerCategory.setAdapter(sc); 


      List<String> l = new ArrayList<String>(); 
      String[] paymentMode = {"Cash", "Credit/Debit Card", "Cheque", "NetBanking"}; 
      l.add("Cash"); 
      l.add("Credit/Debit Card"); 
      l.add("Cheque"); 
      l.add("NetBanking"); 
      ArrayAdapter<String> sp = new ArrayAdapter<String>(this, 
        android.R.layout.simple_spinner_dropdown_item, l); 
      spinnerPayment.setAdapter(sp); 

      save.setOnClickListener(this); 


      spinnerCategory.setOnItemSelectedListener(new OnItemSelectedListener() { 

       public void onItemSelected(AdapterView<?> parent, 
              View selectedItemView, int pos, long id) { 
        spinnerItemSelectedCategory = parent.getItemAtPosition(pos) 
          .toString(); 



       } 

       public void onNothingSelected(AdapterView<?> parentView) { 
        spinnerItemSelectedCategory = "Food"; 
       } 
      }); 

      spinnerPayment.setOnItemSelectedListener(new OnItemSelectedListener() { 

       public void onItemSelected(AdapterView<?> parent, 
              View selectedItemView, int pos, long id) { 
        spinnerItemSelectedPayment = parent.getItemAtPosition(pos).toString(); 

        if (spinnerItemSelectedPayment.equals("Cheque")) { 
         Intent cheque = new Intent(EnterAmount.this, Cheque.class); 
         cheque.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(cheque); 


        } else if (spinnerItemSelectedPayment.equals("Credit/Debit Card")) { 

         Intent card = new Intent(EnterAmount.this, CardNo.class); 
         card.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(card); 


        } 
       } 

       public void onNothingSelected(AdapterView<?> parentView) { 
        spinnerItemSelectedPayment = "Cash"; 

       } 
      }); 


      final Calendar c = Calendar.getInstance(); 

      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy-HH:mm:ss "); 

      date = sdf.format(c.getTime()); 


      int yy = c.get(Calendar.YEAR); 
      int mm = c.get(Calendar.MONTH) + 1; 
      int dd = c.get(Calendar.DAY_OF_MONTH); 

      String s = yy + "" + (mm < 10 ? ("0" + mm) : (mm)) + "" 
        + (dd < 10 ? ("0" + dd) : (dd)); 

      Log.e("datechange", s); 

      date2 = Integer.parseInt(s); 
      Log.e("integer2", "hello" + date2); 

     } 


     @Override 
     public void onBackPressed() { 

      super.onBackPressed(); 
      finish(); 

     } 

    private void vibrate(int ms) { 
     ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(ms); 
    } 


    private void loadSavedPreferences() { 

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

     budget = sharedPreferences.getString("Budget", " "); 

getSharedPreferences(mypreference,Context.MODE_PRIVATE); 
     bankname = sharedPreferences.getString("Bank Name", "Not Applicable"); 
     cardno = sharedPreferences.getString("Card No", "Not Applicable"); 
     chequeno = sharedPreferences.getString("Cheque No", "Not Applicable"); 


    } 

    private void removeSavedPreferences() { 

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.remove("Bank Name"); 
     editor.remove("Cheque No"); 
     editor.remove("Card No"); 
     editor.apply(); 
    } 

    private void savePreferences(String key, String value) { 

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     Editor editor = sharedPreferences.edit(); 
     editor.putString(key, value); 
     editor.commit(); 


    } 


    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.bsaveDb: { 

       savePreferences("Budget", etbdgt.getText().toString()); 
       loadSavedPreferences(); 



       sAmt = etamt.getText().toString(); 


       Log.e("category", "Hello" + sAmt); 
       try { 
        amt = Integer.parseInt(sAmt); 
        Log.e("amt is", "" + amt); 

       } catch (Exception e) { 


       } 


       DbClass dc = new DbClass(this); 
       dc.open(); 

       if (amt == 0) { 

        Toast.makeText(getApplicationContext(), 
          "Please insert the amount", Toast.LENGTH_SHORT).show(); 
       } else { 


        dc.categoryDetailsInsert(amt, spinnerItemSelectedCategory, date, spinnerItemSelectedPayment, date2, bankname, cardno, chequeno); 
        dc.close(); 
        Toast.makeText(getApplicationContext(), "Saved successfully", 
          Toast.LENGTH_LONG).show(); 
        amt = 0; 
        etamt.setText(""); 
        etbdgt.setText(budget); 
        removeSavedPreferences(); 


       } 

       break; 

      } 
     } 
    } 
    } 

私はSQLiteのデータベースのスクリーンショットをattchingています、あなたは銀行名が正しく保存されつつあるが、CARDNOなしを確認するには、常に支払いに関して同じである見ることができます。 Screenshot Of Database

+0

カード決済アクティビティで既に使用されているので、「銀行名」以外の別のキーを使用するか、小切手支払でSharedPreferencesが「いいえ」を選択してください。 –

+0

@voidpointer銀行名に問題はありません。カード決済アクティビティでは「チェックなし」は定義されていません。問題のスクリーンショットが添付されているのを見てください。ちょうど最後の4-5のエントリしか表示されません。 –

答えて

0

実際に上記のコードは問題です。データベースからコードを取得していたコードに問題があり、2つの異なる列に同じインデックス番号を挿入しました。だから、私はチェックナンバーとデビットカード番号に同じ値を得ていた。

0

値を上書きしないようにコンテキストタグにキーを添付します。 のように: 文字列TAG = "ContextNameまたはActivityName";省に続い

は行います

pref.put(TAG +キー、 "値");

+0

と別のアクティビティでこれらの値を取得する方法は? –

関連する問題