title: Save listview to txt file
body: <p>I am trying to save all the items in my ListView to a text file. I am making an item then saving it on this button click.</p>
  
  <pre><code>@Override
  public void onClick(View v) {
      switch(v.getId()) {
  
          case R.id.addItem:
  
              items.add(new ListViewItem() {{
  
                  ThumbnailResource = R.mipmap.ic_launcher;
                  Title = et.getText().toString();
                  SubTitle = "Item2 Description";
  
              }});
              lv.setAdapter(adapter);
  
              // add-write text into file
              try {
                  fileout = openFileOutput("mytextfile.txt", MODE_PRIVATE);
                  outputWriter = new OutputStreamWriter(fileout);
                  outputWriter.write(et.getText().toString());
                  outputWriter.close();
  
                  //display file saved message
                  Toast.makeText(getBaseContext(), "File saved successfully!",
                          Toast.LENGTH_SHORT).show();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          break;
  </code></pre>
  
  <p>Then When the person exits out of the app and goes back in, this code in on create adds the list item back.</p>
  
  <pre><code>  try {
          FileInputStream fileIn=openFileInput("mytextfile.txt");
          InputStreamReader InputRead= new InputStreamReader(fileIn);
  
          char[] inputBuffer= new char[READ_BLOCK_SIZE];
          String start="";
          int charRead;
  
          while ((charRead=InputRead.read(inputBuffer))&gt;0) {
              // char to string conversion
              String readstring=String.copyValueOf(inputBuffer,0,charRead);
              start +=readstring;
          }
          InputRead.close();
          final String finalStart = start;
          items.add(new ListViewItem() {{
  
              ThumbnailResource = R.mipmap.ic_launcher;
              Title = finalStart;
              SubTitle = "Item2 Description";
  
          }});
          lv.setAdapter(adapter);
          Toast.makeText(getBaseContext(), start,Toast.LENGTH_SHORT).show();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>The problem is it saves only one ListView item at a time. not all of them even though it saves after every button click. How do I get it to save all he items to the txt file. Not just the most recent one?</p>
  
  <p>Thanks.</p>
  
  <p>Here is all my code:</p>
  
  <p>MainActivity:</p>
  
  <pre><code> public class MainActivity extends ActionBarActivity implements 
   View.OnClickListener{
  
  EditText et;
  
  Button bt;
  
  ListView lv;
  
  List&lt;ListViewItem&gt; items;
  
  CustomListViewAdapter adapter;
  
  FileOutputStream fileout;
  
  OutputStreamWriter outputWriter;
  
  static final int READ_BLOCK_SIZE = 100;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      et = (EditText) findViewById(R.id.editText);
      bt = (Button) findViewById(R.id.addItem);
      bt.setOnClickListener(this);
  
      lv = (ListView) findViewById(R.id.listView);
      items = new ArrayList&lt;ListViewItem&gt;();
      adapter = new CustomListViewAdapter(this, items);
  
      try {
          FileInputStream fileIn=openFileInput("mytextfile.txt");
          InputStreamReader InputRead= new InputStreamReader(fileIn);
  
          char[] inputBuffer= new char[READ_BLOCK_SIZE];
          String start="";
          int charRead;
  
          while ((charRead=InputRead.read(inputBuffer))&gt;0) {
              // char to string conversion
              String readstring=String.copyValueOf(inputBuffer,0,charRead);
              start +=readstring;
          }
          InputRead.close();
          final String finalStart = start;
          items.add(new ListViewItem() {{
  
              ThumbnailResource = R.mipmap.ic_launcher;
              Title = finalStart;
              SubTitle = "Item2 Description";
  
          }});
          lv.setAdapter(adapter);
          Toast.makeText(getBaseContext(), start,Toast.LENGTH_SHORT).show();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      lv.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
          // setting onItemLongClickListener and passing the position to the function
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
                                         int position, long arg3) {
              removeItemFromList(position);
  
              return true;
          }
      });
  }
  
  @Override
  public void onClick(View v) {
      switch(v.getId()) {
  
          case R.id.addItem:
  
              items.add(new ListViewItem() {{
  
                  ThumbnailResource = R.mipmap.ic_launcher;
                  Title = et.getText().toString();
                  SubTitle = "Item2 Description";
  
              }});
              lv.setAdapter(adapter);
  
              // add-write text into file
              try {
                  fileout = openFileOutput("mytextfile.txt", MODE_PRIVATE);
                  outputWriter = new OutputStreamWriter(fileout);
                  outputWriter.write(et.getText().toString());
                  outputWriter.close();
  
                  //display file saved message
                  Toast.makeText(getBaseContext(), "File saved successfully!",
                          Toast.LENGTH_SHORT).show();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          break;
      }
  
  }
  
  class ListViewItem {
  
          public int ThumbnailResource;
          public String Title;
          public String SubTitle;
  }
  
  protected void removeItemFromList(int position) {
      final int deletePosition = position;
  
      AlertDialog.Builder alert = new AlertDialog.Builder(
              MainActivity.this);
  
      alert.setTitle("Delete");
      alert.setMessage("Do you want delete this item?");
      alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              // TOD O Auto-generated method stub
  
              // main code on after clicking yes
              items.remove(deletePosition);
              adapter.notifyDataSetChanged();
              adapter.notifyDataSetInvalidated();
  
          }
      });
      alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              // TODO Auto-generated method stub
              dialog.dismiss();
          }
      });
  
      alert.show();
  
      }
  }
  </code></pre>
  
  <p>CustomViewAdapter:</p>
  
  <pre><code>public class CustomListViewAdapter extends BaseAdapter {
  
  LayoutInflater inflater;
  List&lt;MainActivity.ListViewItem&gt; items;
  
  public CustomListViewAdapter(Activity context, List&lt;MainActivity.ListViewItem&gt; items) {
      super();
  
      this.items = items;
      this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      return items.size();
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
  
      MainActivity.ListViewItem item = items.get(position);
  
      View vi=convertView;
  
      if(convertView==null)
          vi = inflater.inflate(R.layout.item_row, null);
  
      ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
  
      TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
  
      TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
  
      imgThumbnail.setImageResource(item.ThumbnailResource);
  
      txtTitle.setText(item.Title);
  
      txtSubTitle.setText(item.SubTitle);
  
      return vi;
     }
  }
  </code></pre>