2013-08-30 36 views
1

我開發了一個android應用程序...其中...用戶姓名出生日期,手機號碼,電子郵件ID一切都收集了......我想存儲這些詳細信息...當用戶想要這些數據的時候......一個按鈕被稱爲召回...當用戶按下召回按鈕時..這個數據的存儲位置可以顯示..他可以從這個名字中選擇他的名字...任何人都知道如何保存這些數據....我給代碼...在這裏...任何人請幫助..代碼工作....沒有問題...在android中存儲EditText值

public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) 
     { 

     String date=Integer.toString(arg3); 
     String month=Integer.toString(arg2); 
     String year=Integer.toString(arg1); 

     tv1.setText(date+ month+1 +year); 
     tv2.setText(month+1 + year); 
     tv3.setText(year); 
     tv4.setText(month+1); 
     tv5.setText(date); 
     tv6.setText(date + year); 

     } 

    });} 

    public void sum(View v) 
    { 
     long sum1=0; 
     long sum2=0; 
     long sum3=0; 
     long sum4=0; 
     long sum5=0; 
     long sum6=0; 
     long sum7=0; 
     long sum8=0; 
     long sum=0; 
     long sum11=0; 
     long sum12=0; 
     long sum13=0; 
     long sum14=0; 

     EditText et1 = (EditText) findViewById (R.id.editText1); 
     EditText et2 = (EditText) findViewById (R.id.editText2); 
     EditText et3 = (EditText) findViewById (R.id.editText3); 
     EditText et4 = (EditText) findViewById (R.id.editText4); 

     final DatePicker date = (DatePicker) findViewById (R.id.datePicker1); 
     final TextView tv1 = (TextView) findViewById (R.id.textView88); 
     final TextView tv2 = (TextView) findViewById (R.id.textView89); 
     final TextView tv3 = (TextView) findViewById (R.id.textView90); 
     final TextView tv4 = (TextView) findViewById (R.id.textView91); 
     final TextView tv5 = (TextView) findViewById (R.id.textView92); 
     final TextView tv6 = (TextView) findViewById (R.id.textView93); 
     final TextView tv7 = (TextView) findViewById (R.id.textView94); 

     sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString()); 
     sum2=getSum1(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString()); 
     sum3=getSum2(tv1.getText().toString()); 
     /*sum4=getSum3(tv2.getText().toString());*/ 
     sum5=getSum4(tv3.getText().toString()); 
     sum6=getSum5(tv4.getText().toString()); 
     sum8=getSum7(tv5.getText().toString()); 

     String yearf = tv3.getText().toString(); 
     int yearb = Integer.valueOf(yearf); 

     int val = 2013 - yearb; 
     long val1 = sum3; 
     /*int val2 = Integer.valueOf(val1);*/ 
     long val3 = 36 - val1; 
     int val4 = 9; 
     long val5 = val3 + val4; 
     long val6 = val5 + val4; 
     long val7 = val6 + 50; 

     sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString()); 

     if (val <= val3) 
     { 
      sum4=getSum3(tv2.getText().toString()); 
     } 

     else if ((val <= val5) && (val > val3)) 
     { 
      sum4=getSum8(tv6.getText().toString()); 
     } 

     else if ((val<=val6) && (val > val5) && (val > val3)) 
     { 
      /*sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());*/ 
      sum4=getSum9(Integer.toString((int) sum)); 
     } 
     else 
     { 
      sum4=getSum9(Integer.toString((int) sum)); 
     } 

     if (val <= val3) 
     { 

     if (sum5>sum6) 
     { 
      sum7 = (sum5 - sum6); 
     } 
     else 
     { 
      sum7 = (sum6 - sum5); 
     } 

     } 

     else if ((val <= val5) && (val > val3)) 
     { 
      if(sum5>sum8) 
      { 
       sum7=(sum5 - sum8); 
      } 
      else 
      { 
       sum7 = (sum8 - sum5); 
      } 
     } 


     else if ((val<=val6) && (val > val5) && (val > val3)) 
     { 
      sum11 = (sum5 - sum8); 
      sum12 = (sum8 - sum5); 
      sum13 = (sum6 - sum5); 
      sum14 = (sum5 - sum6); 

      if ((sum11 > sum12) && (sum13 > sum14)) 
      { 
       if (sum11 > sum13) 
       { 
        sum7 = sum11 - sum13; 
       } 
       else 
       { 
        sum7 = sum13 - sum11; 
       } 
      } 
      else if ((sum12 > sum11) && (sum13 > sum14)) 
      { 
       if (sum12 > sum13) 
       { 
        sum7 = sum12 - sum13; 
       } 
       else 
       { 
        sum7 = sum13 - sum12; 
       } 
      } 
      else if ((sum11 > sum12) && (sum14 > sum13)) 
      { 
       if (sum11 > sum14) 
       { 
        sum7 = sum11 - sum14; 
       } 
       else 
       { 
        sum7 = sum14 - sum11; 
       } 
      } 
      else if ((sum12 > sum11) && (sum14 > sum13)) 
      { 
       if (sum12 > sum14) 
       { 
        sum7 = sum12 - sum14; 
       } 
       else 
       { 
        sum7 = sum14 - sum12; 
       } 
      } 

     } 
      else 
      { 
       sum11 = (sum5 - sum8); 
       sum12 = (sum8 - sum5); 
       sum13 = (sum6 - sum5); 
       sum14 = (sum5 - sum6); 

       if ((sum11 > sum12) && (sum13 > sum14)) 
       { 
        if (sum11 > sum13) 
        { 
         sum7 = sum11 - sum13; 
        } 
        else 
        { 
         sum7 = sum13 - sum11; 
        } 
       } 
       else if ((sum12 > sum11) && (sum13 > sum14)) 
       { 
        if (sum12 > sum13) 
        { 
         sum7 = sum12 - sum13; 
        } 
        else 
        { 
         sum7 = sum13 - sum12; 
        } 
       } 
       else if ((sum11 > sum12) && (sum14 > sum13)) 
       { 
        if (sum11 > sum14) 
        { 
         sum7 = sum11 - sum14; 
        } 
        else 
        { 
         sum7 = sum14 - sum11; 
        } 
       } 
       else if ((sum12 > sum11) && (sum14 > sum13)) 
       { 
        if (sum12 > sum14) 
        { 
         sum7 = sum12 - sum14; 
        } 
        else 
        { 
         sum7 = sum14 - sum12; 
        } 
       } 

      } 


     String edtxt1 = et1.getText().toString(); 
     String edtxt4 = et4.getText().toString(); 

     if(edtxt1.equals("")) 
     { 
      Toast.makeText(getApplicationContext(),"First Name should not be left blank, Please enter your First Name ", Toast.LENGTH_LONG).show(); 
     } 

     else if(edtxt4.equals("")) 
     { 
      Toast.makeText(getApplicationContext(),"Last Name should not be left blank, Please enter your Last Name ", Toast.LENGTH_LONG).show(); 
     } 

     else{ 

     Intent i = new Intent(this, result.class); 
     i.putExtra("name", sum1 + ""); 
     i.putExtra("name1", sum2 + ""); 
     i.putExtra("name2", sum3 + ""); 
     i.putExtra("name3", sum4 + ""); 
     i.putExtra("name4", sum7 + ""); 

     startActivity(i); 

     } 

回答

1

你應該看看Storage Options。但是,如果這不是很多用戶,你可以使用SharedPreferences。該鏈接有一個開始使用它們的好例子。

如果您打算讓多個用戶使用那些截然不同的數據,那麼您可能需要使用SQLite DB。如果你不習慣使用SQLite Db,那麼this tutorial可能是一個很好的開始。

您也可以使用我提供的鏈接中的其他存儲選項,但是數據庫可能是存儲大量針對不同用戶的不同信息的最佳選擇。

1

你有沒有考慮過使用SharedPreferences?

您可以使用SharedPreferences存儲任何變量,這非常簡單! 這裏有一個快速教程

SharedPreferences prefs = this.getSharedPreferences(
     getApplicationContext(), Context.MODE_PRIVATE); 

閱讀喜好:

String dateTimeKey = "com.example.app.datetime"; 
// use a default value using new Date() 
    long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

編輯和保存喜好

Date dt = getSomeDate(); 
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();