1
我有一個活動的列表視圖,我想顯示從文本文件中讀取的信息。這是上述活動的代碼(類):ListView不顯示來自文本文件的文本
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。我其實並沒有完全理解它和它的參數是如何工作的。
怪異的東西。你能發佈你的佈局文件嗎? –
當然,我會在第一次加入它。但是,佈局非常簡單,假設主活動中的按鈕調用啓動ViewData活動的函數。 – JasonStack
您是否嘗試過從onCreate內填入arraylist? – emhomm4