我们已经尝试了您的解决方案,它可以很好地工作,但是所有答案都归功于数组0并将所有内容组合在一起,所以您可以解决该问题
我将其用于收集用户的数据
public class profile extends AppCompatActivity {
EditText name1 , surname1 , idnumber1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
name1 = (EditText)findViewById(R.id.name);
surname1 = (EditText)findViewById(R.id.surname);
idnumber1 = (EditText)findViewById(R.id.Idnumber);
}
public void save(View view){
String name2 = name1.getText().toString();
String surname2 = surname1.getText().toString();
String idnumber2 = idnumber1.getText().toString();
File file = null;
name2 = name2+"";
surname2 = surname2+"";
FileOutputStream fileOutputStream = null;
try {
file = getFilesDir();
fileOutputStream = openFileOutput("Rank It Up.txt" , Context.MODE_PRIVATE);
fileOutputStream.write(name2.getBytes());
fileOutputStream.write(surname2.getBytes());
fileOutputStream.write(idnumber2.getBytes());
} catch (IOException e) {
Log.d("Rank It Up", e.toString());
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(this, "Save successfully"+file+"/Rank It Up.txt", Toast.LENGTH_LONG).show();
}
public void next(View view){
Toast.makeText(this, "Database page",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DBActivity.class);
startActivity(intent);
}
}
,然后将其用于从用户收集的显示数据
public class DBActivity extends AppCompatActivity {
TextView name1 , surname1 , idnumber1;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_db);
name1 = (TextView)findViewById(R.id.name);
surname1 = (TextView)findViewById(R.id.surname);
idnumber1 = (TextView)findViewById(R.id.idnumber);
}
public void show(View view){
try {
FileInputStream fileInputStream = openFileInput("Rank It Up.txt");
int read = -1;
StringBuffer buffer = new StringBuffer();
while ((read=fileInputStream.read()) != -1)
{
buffer.append((char) read);
}
Log.d("Rank It Up",buffer.toString());
String m = buffer.toString();
String[] data = m.split(" ");
String[] data = m.split(" ");
String name2 = (data.length > 0) ? data[0] : "";
String surname2 = (data.length > 1) ? data[1] : "";
String idnumber2 = (data.length > 2) ? data[2] : "";
name1.setText(name2);
surname1.setText(surname2);
idnumber1.setText(idnumber2);
} catch (FileNotFoundException e) {
Toast.makeText(this, "File Not Found", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
Toast.makeText(this, "Data found", Toast.LENGTH_LONG).show();
}
public void back(View view){
Toast.makeText(this, "Main page",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, profile.class);
startActivity(intent);
}
问题是为什么所有数据都在单行上显示
在此处输入图像描述
显然,您的数据存在问题。它可能不包含空格,因此当您用空格拆分时,只能获得1个值。检查,您将在拆分调用后立即看到数据阵列的长度1。但是您无法发现此错误,因为您有以下代码:
String name2 = (data.length > 0) ? data[0] : "";
String surname2 = (data.length > 1) ? data[1] : "";
String idnumber2 = (data.length > 2) ? data[2] : "";
此代码填充了那些空位,尽管它可以防止您的应用程序崩溃,但它也会使您不了解问题在哪里。