Android 在垂直列表中显示内部存储器中的数据
我正在创建一个应用程序,将edittext和spinner的输入存储到内部存储器中。我使用了模式_APPEND,这样存储的信息就不会被覆盖。但是,尽管我将XML方向更改为垂直方向,它还是水平存储的。我如何克服这个问题 主要活动部分:Android 在垂直列表中显示内部存储器中的数据,android,Android,我正在创建一个应用程序,将edittext和spinner的输入存储到内部存储器中。我使用了模式_APPEND,这样存储的信息就不会被覆盖。但是,尽管我将XML方向更改为垂直方向,它还是水平存储的。我如何克服这个问题 主要活动部分: public void Save (View view){ // add-write text into file // add-write text into file try { FileOutputStream
public void Save (View view){
// add-write text into file
// add-write text into file
try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE | MODE_APPEND);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(edDate.getText().toString());
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileOutputStream fileout=openFileOutput("mytextfile2.txt", MODE_PRIVATE | MODE_APPEND);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(spinner.getSelectedItem().toString());
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
RecordDisplay.xml
<android.support.constraint.ConstraintLayout 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"
tools:context=".recordsdisplay"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Date:"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_marginTop="16dp"
android:textColor="#000000"
android:textSize="15sp"
android:visibility="visible"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Results:"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textViewResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:textColor="#000"
android:textSize="15sp"
android:visibility="visible"
app:layout_constraintBaseline_toBaselineOf="@+id/textView3"
app:layout_constraintStart_toEndOf="@+id/textView3" />
您可以将
ConstraintLayout
更改为LinearLayout
。这有一个限制,即在创建应用程序时,XML文件必须定义确切的行数。如果用户可以添加数据和任意多的行,则应改用ListView
或RecyclerView
。查看其他布局选项。您应该使用列表视图
或回收视图
,而不是一堆文本视图
s。
TextView date,Results;
FileInputStream fileInputStream;
static final int READ_BLOCK_SIZE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recordsdisplay);
date = (TextView) findViewById(R.id.textViewDate);
Results = (TextView) findViewById(R.id.textViewResults);
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
date.setText("Date:" +s);
} catch (Exception e) {
e.printStackTrace();
}
try {
FileInputStream fileIn=openFileInput("mytextfile2.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String b="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
b +=readstring;
}
InputRead.close();
Results.setText("Results:" + b);
} catch (Exception e) {
e.printStackTrace();
}
}
}