2 qq 33721362 qq_33721362 于 2016.01.31 07:13 提问

怎么只写一个SharedPreferences让它保存多个数据,而不用像下面代码一样写多个SharedPreferences来保存数据

package com.myedit;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.content.*;

public class MainActivity extends Activity
{

private SharedPreferences sp,spa,spb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText a1=(EditText)findViewById(R.id.a1);
    final EditText a2=(EditText)findViewById(R.id.a2);
    final EditText a3=(EditText)findViewById(R.id.a3);
    Button   a4=(Button)findViewById(R.id.a4);
    final TextView a5=(TextView)findViewById(R.id.a5);
    final TextView a6=(TextView)findViewById(R.id.a6);
    final TextView a7=(TextView)findViewById(R.id.a7);
//保存内容
sp=getSharedPreferences("ba",MODE_PRIVATE);
spa=getSharedPreferences("bb",MODE_PRIVATE);
spb=getSharedPreferences("bc",MODE_PRIVATE);
String str=sp.getString("name","");
String s=spa.getString("name","");
String st=spb.getString("name","");
a5.setText(str);
a6.setText(s);
a7.setText(st);
    a4.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                a5.setText(a1.getText().toString());
                a6.setText(a2.getText().toString());
                a7.setText(a3.getText().toString());
                //读取保存内容



                sp.edit().putString("name",a5.getText().toString()).commit();
                spa.edit().putString("name",a6.getText().toString()).commit();
                spb.edit().putString("name",a7.getText().toString()).commit();

            }
        });
}

}
图片图片图片图片

2个回答

caozhy
caozhy   Ds   Rxr 2016.01.31 07:24
 private SharedPreferences sp,spa,spb;
->
private SharedPreferences sp;

sp=getSharedPreferences("ba",MODE_PRIVATE);
//去掉spa=getSharedPreferences("bb",MODE_PRIVATE);
//去掉spb=getSharedPreferences("bc",MODE_PRIVATE);

String xx = sp.getString("name","");
String str=xx.split(",")[0];
String s=xx.split(",")[1];
String st=xx.split(",")[2];

sp.edit().putString("name",a5.getText().toString() + "," + a6.getText().toString() + "," + a7.getText().toString()).commit();
//去掉spa.edit().putString("name",a6.getText().toString()).commit();
//去掉spb.edit().putString("name",a7.getText().toString()).commit();
qq_33721362
qq_33721362 能给出完整的方法吗?这split函数不明白
接近 2 年之前 回复
caozhy
caozhy 回复qq_33721362: 首先我的代码你看懂没有?用一个变量存储3个变量。中间用逗号分割。如果你原先的数据不是这样的,会因为split分割出来没有3个报错。你修改下就可以了。
接近 2 年之前 回复
qq_33721362
qq_33721362 我问了一下别人,说是什么数组角标错误,那是?
接近 2 年之前 回复
caozhy
caozhy 回复qq_33721362: 你完全按照我修改的么?另外,你原先的配置文件也要修改,把它们放在一起,用逗号分割
接近 2 年之前 回复
qq_33721362
qq_33721362 把那多余的去掉了,加入你说的,但停止运行⊙▽⊙
接近 2 年之前 回复
u010539561
u010539561   2016.02.02 16:23

public class MainActivity extends Activity
{

 private SharedPreferences sp,spa,spb;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText a1=(EditText)findViewById(R.id.a1);
final EditText a2=(EditText)findViewById(R.id.a2);
final EditText a3=(EditText)findViewById(R.id.a3);

Button   a4=(Button)findViewById(R.id.a4);

final TextView a5=(TextView)findViewById(R.id.a5);
final TextView a6=(TextView)findViewById(R.id.a6);
final TextView a7=(TextView)findViewById(R.id.a7);

    //保存内容
    sp=getSharedPreferences("ba",MODE_PRIVATE);

    String xx = sp.getString("name","");

    String str=xx.split(",")[0];
    String s=xx.split(",")[1];
    String st=xx.split(",")[2];

    a5.setText(str);
    a6.setText(s);
    a7.setText(st);

a4.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View p1)
        {
            // TODO: Implement this method
            a5.setText(a1.getText().toString());
            a6.setText(a2.getText().toString());
            a7.setText(a3.getText().toString());
            //读取保存内容
            String info=a5.getText().toString() + "," + a6.getText().toString() + "," + a7.getText().toString();
           sp.edit().putString("name",saveInfo).commit();

        }
    });
}

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!