0

I am currently using a StreamReader to open a .txt file from the Assets which contains lines of strings into a ListView

How would I make it so that when I tap on a list view item, it deletes the line from the text File?

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();

    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
CodePlague
  • 89
  • 1
  • 10

1 Answers1

3

Assets.Open("NotesData.txt"); I assume that you open file from the application assets. Application assets can't be modified. What you can modify is files in the file system. if you would want to do the same with the file in the file system, just remove the line with the clicked position from the List<string> lines when clicking on the item, and overwrite these lines into the file.

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • Where do I put the file, inside the File System? Which is where? – CodePlague May 19 '17 at 05:37
  • You can copy them for instance to the SD card. See here: http://stackoverflow.com/questions/3845227/how-can-i-edit-the-text-files-in-assets-folder-in-android – Quality Catalyst May 19 '17 at 05:39
  • @CodePlague, for example into your application's internal storage memory, or into SD card... Or wherever you want. The deal is that instead of using it from assets, you extract it to file system on the first launch of the app, and use it from file system after that. – Vladyslav Matviienko May 19 '17 at 05:39
  • Oh, I see... What directory would I put that in inside Xamarin / Visual Studio if I wanted to use the Applications Internal Memory? – CodePlague May 19 '17 at 05:41
  • @CodePlague, first you should put it into assets, as it is now. Then, when first launching the app - extract it from assets to device memory – Vladyslav Matviienko May 19 '17 at 05:45
  • I thought Assets extract to memory themselves... If not, how is this done? There is no documentation on it... – CodePlague May 19 '17 at 05:49
  • @CodePlague, Assets are not extracted, since they are a part of application, which is stored as APK, and is not extracted when installing. So you were able to load the file from assets into a list of lines. Now you can save this list of lines into the device memory. That will be a kind of extraction. – Vladyslav Matviienko May 19 '17 at 05:59
  • How would I delete a String from the `List lines` and overwrite the file with the deleted String gone? – CodePlague May 20 '17 at 07:19
  • to remove item from list: https://www.google.com.ua/search?q=c%23+remove+from+list to write to file: https://www.google.com.ua/search?q=c%23+write+list+of+strings+to+file – Vladyslav Matviienko May 20 '17 at 07:42