可爱的老师,求助

可爱的老师,求助

嗨老师,我在尝试一个功能,就是把输入的EditText里做永久保存。也就是说,用户输入之后,就算关掉软件,重启,用户之前填的东西也不会丢。

然后就出现一个问题,我把save 和 load 方法提取出来。然后,使用的时候,出现了一个bug..就是,我有两个EditText,但是取出来的内容,只能 取到最后一个保存的,以下为详细代码,方便老师做实验,请帮我看下哈,谢谢您。


我提取出来的save 和 load方法:

package com.example.edittestapplication.utils;



import com.example.edittestapplication.GRApplication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import static android.content.Context.MODE_PRIVATE;


public class LongConserve {

/**
* save method
*/
public static void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;

try {
out = GRApplication.getContext().openFileOutput("data",MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


/**
* load method
* @param describe
*/
public static String load(String describe) {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();

try {
in = GRApplication.getContext().openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
content.append(line);

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();


}

}


这个contex,我弄了一个全局的:

package com.example.edittestapplication;

import android.app.Application;
import android.content.Context;

public class GRApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();


}

public static Context getContext() {
return context;
}
}


接着是

MainActivity的Layout:

http://img1.sycdn.imooc.com//climg/5fb59af709e902aa05150144.jpg

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/Tv_name"
android:text="姓名"
android:textSize="15dp"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Tv_describe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="describe"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:textSize="15dp"/>



</LinearLayout>


然后是EditActivity的Layout:

http://img1.sycdn.imooc.com//climg/5fb59b3909f0c84f05020232.jpg

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".EditActivity">
<EditText
android:id="@+id/edt_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="name"
android:textColorHint="#707070"
android:textColor="#000000"
android:textSize="15dp"/>
<EditText
android:id="@+id/edt_describe"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:textColorHint="#707070"
android:hint="Describe"/>
<Button
android:id="@+id/sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="#000000"/>

</LinearLayout>


MainActivity的主代码

package com.example.edittestapplication;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView Tv_name,Tv_describe;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tv_name = findViewById(R.id.Tv_name);
Tv_describe = findViewById(R.id.Tv_describe);
initEvent();
}

private void initEvent() {
Tv_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this,EditActivity.class);
startActivityForResult(intent1,1);
}
});

Tv_describe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(MainActivity.this,EditActivity.class);
startActivityForResult(intent2,2);
}
});



}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
if(resultCode == RESULT_OK){
User user = data.getParcelableExtra("return_userName_userDescribe");
String userName = user.getName();
String userDescribe = user.getDescribe();
Tv_name.setText(userName);
Tv_describe.setText(userDescribe);


}
break;

case 2:
if(resultCode == RESULT_OK){
User user = data.getParcelableExtra("return_userName_userDescribe");
String userName = user.getName();
String userDescribe = user.getDescribe();
Tv_describe.setText(userDescribe);
Tv_name.setText(userName);
}
break;
default:

}
}
}


EditActivity的主代码:

package com.example.edittestapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.edittestapplication.utils.LongConserve;


public class EditActivity extends AppCompatActivity {
private EditText edt_name,edt_describe;
private Button sure;
private String name;
private String intro;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
initView();
initEvent();
}



private void initView() {
edt_name = findViewById(R.id.edt_name);
edt_describe = findViewById(R.id.edt_describe);
sure = findViewById(R.id.sure);

if(TextUtils.isEmpty(name)){
String inputName = LongConserve.load(name);
edt_name.setText(inputName);
}

if(TextUtils.isEmpty(intro)){
String inputDes = LongConserve.load(intro);
edt_describe.setText(inputDes);
}


}

private void initEvent() {
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

User user = new User();
name = edt_name.getText().toString();
intro = edt_describe.getText().toString();
user.setName(name);
user.setDescribe(intro);
Intent intent = new Intent(EditActivity.this,MainActivity.class);
intent.putExtra("return_userName_userDescribe",user);
setResult(RESULT_OK,intent);
finish();

}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
name = edt_name.getText().toString();
LongConserve.save(name);

intro = edt_describe.getText().toString();
LongConserve.save(intro);
}
}


User 的类

​package com.example.edittestapplication;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {
private String name;
private String describe;

public User() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescribe() {
return describe;
}

public void setDescribe(String describe) {
this.describe = describe;
}

protected User(Parcel in) {
name = in.readString();
describe = in.readString();
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
User user = new User();
user.name = source.readString();
user.describe = source.readString();
return user;
}

@Override
public User[] newArray(int size) {
return new User[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(describe);
}
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,由于同学是直接保存name和intro的

http://img1.sycdn.imooc.com//climg/5fb5d1640960863403940216.jpg

在存储时并没有区分是哪个字段值,并且这种方式打开的文件,就是直接覆盖上次内容,

http://img1.sycdn.imooc.com//climg/5fb5d1a209cd6c1405650494.jpg

建议同学用SharedPreferences文件来保存你的两个字段值,以key,value的形式存储,能区分开当前保存的是哪一个字段。祝:学习愉快

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师