我有一个活动列表视图,我希望显示从文本文件中读取的信息。这是所述活动(类)的代码:
public class ViewData extends AppCompatActivity {
//code to show strings from file
private ArrayList<String> readFromFile(Context context) {
ArrayList<String> receiveString=new ArrayList<String>( );
try {
InputStream inputStream = context.openFileInput(AddDataActivity.FileName);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tempString="";
while ((tempString=bufferedReader.readLine()) != null) {
receiveString.add(tempString);
}
inputStream.close();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return receiveString;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
ListView listView=(ListView)findViewById(R.id.listView);
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,readFromFile(getApplicationContext()));
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
// listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
//
// // ListView Clicked item index
// int itemPosition = position;
//
// // ListView Clicked item value
// String itemValue = (String) listView.getItemAtPosition(position);
//
// // Show Alert
// Toast.makeText(getApplicationContext(),
// "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
// .show();
//
// }
//
// });
}
}
我有一个菜单按钮,应该触发此活动(启动它),在onCreate中,我想从文件中读取并显示我保存到另一个活动中的文件的文本内容。但是,我目前看到以下结果,无论对提到的文本文件保存哪些更改,这似乎总是相同的:
这是我的布局的内容视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="mhd.myupliftingmate.ViewData"
tools:showIn="@layout/activity_view_data">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是调用活动ViewData的主菜单布局:
<menu 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"
tools:context="mhd.myupliftingmate.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/settings"
app:showAsAction="ifRoom"
/>
<item app:showAsAction="ifRoom"
android:orderInCategory="101"
android:title="Add Data"
android:id="@+id/action_addData"
android:onClick="openAddActivity"
android:icon="@drawable/input"
/>
<item
android:orderInCategory="102"
android:title="View Data"
android:id="@+id/action_viewData"
android:onClick="openViewActivity"
android:icon="@drawable/text"
app:showAsAction="ifRoom"
/>
</menu>
我也在添加文件编写代码,以防可能出现问题。
public class AddDataActivity extends AppCompatActivity {
public static final String FileName="data.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void saveToFile(View view) {
// private void writeToFile(String data,Context context) {
Context context=getApplicationContext();
View userText=(EditText) findViewById(R.id.userText);
String inputString=userText.toString();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(FileName, Context.MODE_PRIVATE));
outputStreamWriter.write(inputString);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
}
我不知道是否需要实现我的ArrayAdapter版本。我实际上并不完全理解它和它的参数是如何工作的。
答案 0 :(得分:1)
更改
String inputString=userText.toString();
到
String inputString=userText.getText().toString();