我们已经尝试过您的解决方案,它运行良好但所有答案都归于数组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);
}
问题是为什么所有数据都显示在一行
答案 0 :(得分:0)
显然,您的数据存在问题。它可能不包含空格,因此当您使用空格拆分时,您只能获得1个值。检查并在拆分调用后立即看到数据数组的长度为1.但是由于您有以下代码,因此无法发现此错误:
String name2 = (data.length > 0) ? data[0] : "";
String surname2 = (data.length > 1) ? data[1] : "";
String idnumber2 = (data.length > 2) ? data[2] : "";
此代码填补了这些空白点,虽然它可以防止您的应用崩溃,但也会导致您无法理解问题所在。