我目前正在使用StreamReader
打开资产中的.txt
文件,该文件包含ListView
如何点击列表视图项,它会从文本文件中删除该行?
public class MainActivity : Activity
{
ListView notesList;
String[] notesArray;
Stream stream;
StreamReader streamReader;
String line;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
//Notes
notesList = FindViewById<ListView>(Resource.Id.lvNotes);
//File name
stream = Assets.Open("NotesData.txt");
// Declare new List.
List<string> lines = new List<string>();
// Use using StreamReader for disposing.
using (streamReader = new StreamReader(stream))
{
while ((line = streamReader.ReadLine()) != null)
{
lines.Add(line);
}
}
notesArray = lines.ToArray();
//Load ListView Data
ArrayAdapter adapter = new ArrayAdapter(
this, Android.Resource.Layout.SimpleExpandableListItem1, notesArray);
notesList.Adapter = adapter;
notesList.ItemClick += lvNotes_ItemSelected;
}
private void lvNotes_ItemSelected(object sender, AdapterView.ItemClickEventArgs e)
{
//Where I want the Line to be deleted!
string toast = "Clicked: " + notesList.GetItemAtPosition(e.Position);
Toast.MakeText(this, toast, ToastLength.Long).Show();
}
}
答案 0 :(得分:3)
Assets.Open("NotesData.txt");
我假设您从应用程序资产中打开文件。无法修改应用程序资产。您可以修改的是文件系统中的文件。如果您想对文件系统中的文件执行相同操作,只需在单击项目时从List<string> lines
中删除带有单击位置的行,并将这些行覆盖到文件中。