0

I have a list of ItemCodes that are saved in a CSV file in my raw folder in my android application.

In order to eradicate human error, I would like it so that when the user starts typing the edittext will autocomplete based on the itemcodes in the CSV file.

Does anyone know if this is possible?

Here is what I have so far:

 private void ReadItemCodes() {
    InputStream IS = getResources().openRawResource(R.raw.itemcodes);
    BufferedReader reader = new BufferedReader(new InputStreamReader(IS, Charset.forName("UTF-8")));

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            //Split by commas
            String[] tokens = line.split(",");

            //Read the data
            ItemCodes+=line;  
      }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aaron
  • 47
  • 11
  • Possible duplicate of [How to parse CSV file into an array in Android Studio](https://stackoverflow.com/questions/38415680/how-to-parse-csv-file-into-an-array-in-android-studio) – Abhinav Suman Jul 25 '18 at 11:27

2 Answers2

0

If you are just looking for the Suggestion for the AutoComplete Edittext then you can use the string-array, it is easier and if you are having some complex Data Structure then CSV is also good.

if you are looking to do it by using CSV then please follow the needful steps to read the CSV file.

Here you can refer this post https://stackoverflow.com/a/38415815/5343866

Abhinav Suman
  • 940
  • 1
  • 9
  • 29
  • Hi @AbhinavSuman.. ive got as far as reading the CSV file to the list. However, in the adapter for the AutocompleteTextview the last parameter must be a String[]. How do I convert? – Aaron Jul 25 '18 at 13:05
  • So in which data type you are getting the data from CSV file. If you are getting it in the list then convert it to String[]. – Abhinav Suman Jul 25 '18 at 13:09
  • thanks for the help. I was able to find a solution. see below! – Aaron Jul 25 '18 at 14:39
0

For anyone who comes across this in the future. This was the best and simplest approach that I found!

 // Get a reference to the AutoCompleteTextView in the layout
    itemcodetextview = (AutoCompleteTextView) findViewById(R.id.itemcode);
    batchnumbertextview = (AutoCompleteTextView) findViewById(R.id.batchnumber);

    //READ FROM BATCH NUMBER CSV
    Scanner scanner = new Scanner(getResources().openRawResource(R.raw.batchnumbers));
    List<String> listbatchnumbers = new ArrayList<String>();
    while (scanner.hasNext()) {
        listbatchnumbers.add(scanner.next());
    }
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listbatchnumbers );
    batchnumbertextview.setAdapter(adapter);
    scanner.close();

    //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