android存储数组或列表中的值而不覆盖

时间:2015-02-04 19:11:13

标签: android arrays database list sharedpreferences

我只是想知道是否有办法将数据保存到文本文件中而不会被覆盖。

目前我正在使用共享偏好并且它会覆盖可以正常但与此重合的数据,我想要在可以在列表或列形式中覆盖之前保留值的记录。

所以它是这样的: 60,65,70 ..等等 这些值一个接一个地存储而不被覆盖。我想在文本文件中本地执行此操作,也可以阅读。

我这样做,所以我可以创建统计页面或类似的东西。

我希望有人可以帮我找到解决方案。

我的代码:

public class workout extends Activity {

    TextView weightresultText, newweightresultText, difficultyrating;
    Boolean male, strength;
    double newweight, newweight1;
    int weight, age;
    Button button, button4;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.workout);
        button = (Button) findViewById(R.id.button);
        button4 = (Button) findViewById(R.id.button4);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        weight = Integer.parseInt(sharedPreferences.getString("storedweight", "Your Weight"));
        age = Integer.parseInt(sharedPreferences.getString("storedage", "Your Age"));
        male = sharedPreferences.getBoolean("is_male", false);
        strength = sharedPreferences.getBoolean("is_strength", false);

        weightresultText = (TextView) findViewById(R.id.weightresultLabel);
        newweightresultText = (TextView) findViewById(R.id.newweightresultLabel);
        difficultyrating = (TextView) findViewById(R.id.difficultylevel);

         if (sharedPreferences.getBoolean("firstrun", true)) {AgeCalculation();

             sharedPreferences.edit().putBoolean("firstrun", false).commit();}


        AfterFirstLaunch();
    }

    public void SaveWeight(){
        savePreferences("storednewweight", (Double.toString(newweight)));
    }

//Should only happen on first launch.
    public void AgeCalculation() {
        if (strength == true){
        if (male == true && age >= 18) {
            newweight = (weight * 0.8);
            }
        if (male == true && age >= 30) {
            newweight = (weight * 0.6);
        }
        if (male == true && age >= 50) {
            newweight = (weight * 0.4);
        }
        if (male == true && age >= 70) {
            newweight = (weight * 0.2);
        }
        if (male == true && age > 80) {
            newweight = (weight * 0.1);
        }
        if (male == false && age >= 20 ){
            newweight = (weight * 0.3 );
                    }
        weightresultText.setText(Double.toString(newweight));
        SaveWeight();
            }}

    private void savePreferences(String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

// On every other launch, it is based on button, the newweight is saved and loaded each time.
    public void buttonClick1(){
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            //this gets the current weight based on first time launch.
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String newweight =  sharedPreferences.getString("storednewweight", "");

           newweight1 = Double.parseDouble(newweight);

            newweight1 = newweight1 + 5;
            //saves value into sharedpreference
            savePreferences("storednewweight", (Double.toString(newweight1)));
            //save to shared preference, and load value on new launch?
            //also store into local database for review later.
            difficultyrating.setText("1");
            button.setEnabled(false);
            button4.setEnabled(false);

           //save this data then on new launch it uses this figure.
            // so first time it creates then uses this figure for future.
        }

    });}

    public void buttonClick4(){
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                //this gets the current weight based on first time launch.
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                String newweight =  sharedPreferences.getString("storednewweight", "");

                newweight1 = Double.parseDouble(newweight);

                newweight1 = newweight1 + 10;
                //saves value into sharedpreference
                //save to text file in array
                savePreferences("storednewweight", (Double.toString(newweight1)));

                difficultyrating.setText("2");
                button.setEnabled(false);
                button4.setEnabled(false);

                //save this data then on new launch it uses this figure.
                // so first time it creates then uses this figure for future.
            }

        });}

    //runs on every other launch
    public void AfterFirstLaunch(){

        buttonClick1();
        buttonClick4();
        //receive value on other launches and show on field.
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String newweight =  sharedPreferences.getString("storednewweight", "");
        weightresultText.setText(newweight);

    }



        }

2 个答案:

答案 0 :(得分:0)

要使用覆盖写入文本文件,请使用以下代码

File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/SO/";

File dir = new File(path);
    if (!dir.exists()) {
        if (dir.mkdirs()) {

    }
}

File logFile = new File(path + "filename.txt");
        if (!logFile.exists()) {
            logFile.createNewFile();
        }

        // BufferedWriter for performance, true to set append to file

        FileWriter fw = new FileWriter(logFile, true);
        BufferedWriter buf = new BufferedWriter(fw);


        buf.append("Message");
        buf.newLine();
        buf.flush();


        buf.close();

确保您已在AndroidManifest.xml

中定义了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

对于类似这样的内容,您要存储多个类似数据的条目,您真的希望将SQL数据库与内容提供程序一起使用。您只需为每个条目创建新行。