2016-08-27 5 views
1

Le programme fonctionne bien mais je ne peux pas comprendre pourquoi il ne lira et n'écrira qu'une ligne. Il devrait être capable d'écrire plusieurs lignes et de les lire dans le textview. Je l'ai configuration de sorte que lorsque l'utilisateur clique ajouter qu'il devrait automatiquement lire dans le textviewLira et écrira seulement une ligne

public class MainActivity extends AppCompatActivity { 
EditText Activity, Miles, Date; 
TextView Log; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Activity = (EditText)(findViewById(R.id.editText)); 
    Miles = (EditText)(findViewById(R.id.editText2)); 
    Date = (EditText)(findViewById(R.id.editText3)); 
    Log = (TextView)(findViewById(R.id.textView)); 
} 
public void Add(View view) 
{ 
    String Myactivity = Activity.getText().toString() + "\t" + Miles.getText().toString() + "\t" + Date.getText().toString(); 
    try { 
     FileOutputStream fileOutputStream = openFileOutput("myActivities.txt", MODE_PRIVATE); 
     fileOutputStream.write(Myactivity.getBytes()); 
     fileOutputStream.close(); 
     Toast.makeText(getApplicationContext(), "Activty Added", Toast.LENGTH_LONG); 
     FileInputStream fileInputStream = openFileInput("myActivities.txt"); 
     InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); 
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
     StringBuffer stringBuffer = new StringBuffer(); 
     String action; 
     while((action = bufferedReader.readLine())!= null) 
     { 
      stringBuffer.append(action + "\n"); 
     } 
     Log.setText(stringBuffer.toString()); 

    } catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG); 
    } 


} 

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="mbl404.phoenix.edu.week2appgk5343.MainActivity"> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText" 
    android:hint="Please Enter Type of Workout" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignEnd="@+id/editText2" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText2" 
    android:hint="Please Enter Number of Miles" 
    android:layout_below="@+id/editText" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText3" 
    android:hint="Please Enter Date" 
    android:layout_below="@+id/editText2" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Add" 
    android:id="@+id/button" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="Add" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView" 
    android:layout_below="@+id/button" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentStart="true" /> 
</RelativeLayout> 
+0

modifier votre question et de montrer fichier layout.activity_main.xml –

Répondre

0

Ajouter dans votre textview xml

android:maxLines="100" 

Dans votre code :

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView" 
    android:layout_below="@+id/button" 
    android:maxLines="100" <!-- add android:maxLines to specify the number of lines in textview --> 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentStart="true" /> 

Mise à jour:

while((action = bufferedReader.readLine())!= null) 
    { 
     Log.setText(Log.getText() + action + "\n"); 
    } 
+0

a ajouté que, mais il remplace toujours la ligne actuelle avec la nouvelle ligne. Ne pas ajouter une autre ligne –

+0

J'ai mis à jour ma réponse s'il vous plaît vérifier ceci et la réponse à moi –

+0

Ce code n'a pas fonctionné. Cependant, cela m'a donné une idée: stringBuffer.append (Log.getText(). ToString() + action + "\ n"); cela fonctionne parfaitement –