0

I have 2 CSV's files currently saved in the raw folder within my Android Project. The reason I have these is that I have 2 AutoCompleteTextView fields which read from these CSV files.

The thing is as time goes on these CSV files will be come outdated. So what I would like to achieve is that if the value entered does not exist in the CSV file then it is added to it.

Is this possible?

Thanks!

CSV Files

Write Code:

public void exportEmailInCSV() throws IOException {
    {
        File folder = new File(Environment.getExternalStorageDirectory()
                + "/Folder");
        boolean var = false;
        if (!folder.exists())
            var = folder.mkdir();
        final String filename = folder.toString() + "/" + "Test.csv";
        try {
            FileWriter fw = new FileWriter(filename, true);
            fw.append(batchNumber);
            Toast.makeText(this, "SAVED", Toast.LENGTH_SHORT).show();
            // fw.flush();
            fw.close();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }
}

Read Code:

 public void ReadInCSV() throws IOException {
    {
        BufferedReader br = null;
        File folder = new File(Environment.getExternalStorageDirectory()
                + "/Batchnumbers");
        final String filename = folder.toString() + "/" + "BatchNumbers";

        try {
            br = new BufferedReader(new FileReader(filename + ".csv"));
            while ((sCurrentLine = br.readLine()) != null) {
                Toast.makeText(this, sCurrentLine, Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

AutocompleteTextView code:

//READ FROM ITEM CODES CSV
    Scanner scanner2 = new Scanner(getResources().openRawResource(R.raw.itemcodesofficial));
    List<String> listitemcodes = new ArrayList<String>();
    while (scanner2.hasNext()) {
        listitemcodes.add(scanner2.next());
    }
    ArrayAdapter<String> adapter2 =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listitemcodes );
    itemcodetextview.setAdapter(adapter2);
    scanner2.close();
Aaron
  • 47
  • 11
  • you are tring to edit csv in raw folder? then its not possible. you can copy those files to internal memory and append with new values. – Geo Aug 03 '18 at 11:01
  • https://stackoverflow.com/questions/11341931/how-to-create-a-csv-on-android – Geo Aug 03 '18 at 11:03
  • Thanks @GeorgePJ. It is now writing to a CSV file. Could you advise me on how I can copy and paste values into internal storage? and it also seems to be replacing the value instead of adding a new one when I save. – Aaron Aug 03 '18 at 12:44
  • Can you show me the code you are using to write file? – Geo Aug 03 '18 at 12:47
  • Added above :)! – Aaron Aug 03 '18 at 12:50
  • @GeorgePJ it isn't replacing it, its adding it to the end, when really I want that to be a separate entry? – Aaron Aug 03 '18 at 12:52
  • separate entry means? new line? – Geo Aug 03 '18 at 12:55
  • yeap because I want to read these back to my AutocompleteTextView – Aaron Aug 03 '18 at 12:55
  • 1
    try fw.append("\n"); before write – Geo Aug 03 '18 at 12:57
  • instead of .csv files, why you don't make a database with 2 tables. On first time run, copy data from raw to it (there are some great tuts for it). Word insertion/deletion/update will be much ease for you then. – isamirkhaan1 Aug 03 '18 at 12:58
  • that worked @GeorgePJ. Thanks samirk433 I will have a look at ha – Aaron Aug 03 '18 at 13:03
  • @GeorgePJ. Whats the best way to read these back to AutcompleteTextView? I have added the code I am using to read back and how I am populating the autocomplete – Aaron Aug 03 '18 at 13:14
  • 1
    @Aaron use AsyncTask to read file, make array list of entries and return arraylist. then use arraylist received on `onPostExecute()` to populate autocomplete – Geo Aug 03 '18 at 13:29
  • @Aaron if my comments helpful to you please mark it as useful – Geo Aug 03 '18 at 13:51

0 Answers0