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