2016-12-24 22 views
0

我們已經試過您的解決方案,它很好地工作,但所有的答案去陣列0和結合在一起一切,所以你能解決這個問題我的應用程序的答案去陣列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); 

} 

的問題是,爲什麼所有的數據都顯示在一行

enter image description here

回答

0

顯然,這是你的數據有問題。它可能不包含空格,所以當你用空格分隔它時,你只能得到1個值。檢查,你會看到分裂從那之後,調用數據數組的長度爲1,但因爲你有下面的代碼,你不能當場這個錯誤:

String name2 = (data.length > 0) ? data[0] : ""; 
String surname2 = (data.length > 1) ? data[1] : ""; 
String idnumber2 = (data.length > 2) ? data[2] : ""; 

此代碼填補這些空白點,雖然它防止您的應用程序崩潰,這也會導致您無法瞭解問題出在哪裏。

+0

謝謝你的回答。 – RankItUpProject

+0

我可以成功。非常感謝你。 – RankItUpProject

相關問題