on按钮单击重新启动后从TextView中删除现有数据

时间:2014-06-07 10:20:37

标签: android sharedpreferences

每当我重新启动我的应用程序时,如果我存储了一些旧版本的值,但是面对一个小问题,现在重新启动我的应用程序后,当我点击按钮时,它会删除早先存储的数据并显示新的数据。

我在程序中使用SharedPreferences来存储textview数据,这对我来说很合适。

我可以知道我在哪里做错了吗?

请参阅下面的代码:

public class MainActivity extends ActionBarActivity {

    TextView textViewResult;
    EditText editTextInput;
    String strInput = "";
    Button btnInput;

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

        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);

        textViewResult = (TextView) findViewById(R.id.textResult);
        editTextInput = (EditText) findViewById(R.id.editInput);

        textViewResult.setText(prefs.getString("autoSave", ""));

        textViewResult.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                prefs.edit().putString("autoSave", s.toString()).commit();
            }
        });

        btnInput = (Button) findViewById(R.id.button1);
        btnInput.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String check = editTextInput.getText().toString();
                    if(check.equalsIgnoreCase("ABC"))
                    {
                            strInput = strInput+","+check;

                            Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                            textViewResult.setText(strInput);
                            editTextInput.setText("");
                    }
                    else {
                            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();                    
                    }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个..

btnInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String check = editTextInput.getText().toString();
                if(check.equalsIgnoreCase("ABC"))
                {
                        strInput = textViewResult.getText().toString()+","+check; 

                        Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                        textViewResult.setText(strInput);
                        editTextInput.setText("");
                }
                else {
                        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();                    
                }
        }
    });

修改

使用split("")

String[] total_abc = textViewResult.getText().toString().split(",");
int total = total_abc.length;

答案 1 :(得分:0)

put prefs.edit()。putString(" autoSave",s.toString())。commit();在OnClick事件上

并删除textViewResult.setText(prefs.getString(" autoSave","")); 而不是它写textViewResult.setText(prefs.getString(" autoSave")); 你只需要从共享偏好中指定键值,这就是为什么你得空白