Tuesday, 27 August 2013

Reading CSV file into SQLite into ListView (SimpleAdapter). It's terribly slow. Am I doing this right?

Reading CSV file into SQLite into ListView (SimpleAdapter). It's terribly
slow. Am I doing this right?

I'm kind of new to handling data with multiple kinds of storages, and I'm
not sure if I'm doing this right.
So I have a CSV file with 6 rows, and ~20 columns. I parse it, then read
it into a database. (using OpenCSV)
When I have this database, I create a hash map of it, and use a
SimpleAdapter to display it in a ListView.
Said listview has items which are pretty complex, each of them has around
~20 textviews ("click to expand"-y thing)
The whole thing of course runs depending if the database has already been
created or not.
so if database already exists --> only read the database and do the list
mapping if database doesnt exist, do the whole CSV->database->list job...
My problem is that it's REALLY slow. Doing only 6 rows takes up 13.1777
milliseconds. And this is only my test CSV file, the final app will have
to handle hundreds of rows.
So my question would be simple:
Can I make this thing any faster? I feel like I'm overcomplicating this.
here is some of my code:
Here am I calling the two functions that do the job:
public void showEszkoz() throws Exception {
Cursor c = databaseEszkoz.selectRecords();
System.out.println("row count: " + c.getCount());
if (c.getCount() == 0)
{
System.out.println("read csv to database to list");
readCsvIntoDatabase();
readDatabaseToList();
}
else
{
System.out.println("only database read to list");
readDatabaseToList();
}
}
This the function that reads the CSV files:
public void readCsvIntoDatabase() throws IOException {
InputStream is = getResources().openRawResource(R.raw.eszkoz);
Reader myReader = null;
try {
myReader = new InputStreamReader(is, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
CSVReader reader = new CSVReader(myReader);
reader.readNext();
try {
while ((nextLine = reader.readNext()) != null) {
databaseEszkoz.createRecords(Integer.parseInt(nextLine[0]),
Integer.parseInt(nextLine[1]), nextLine[2],
nextLine[3], nextLine[4], nextLine[5], nextLine[6],
nextLine[7], nextLine[8], nextLine[9], nextLine[10],
nextLine[11], nextLine[12], nextLine[13], nextLine[14],
nextLine[15], nextLine[16], nextLine[17], nextLine[18],
nextLine[19], nextLine[20], nextLine[21], nextLine[22],
nextLine[23], nextLine[24], nextLine[25]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
and this is the function that reads the database and displays it in a list
public void readDatabaseToList() { ArrayList> mylist = new ArrayList>();
HashMap map;
Cursor c = databaseEszkoz.selectRecords();
c.moveToFirst();
while (!c.isAfterLast()) {
System.out.println("This is the current row:" + c.getString(0));
map = new HashMap<String, String>();
map.put("_id", c.getString(0));
map.put("ESZKOZ_ID", c.getString(1));
map.put("ESZKOZ_LELTARSZAM", c.getString(2));
map.put("ESZKOZ_EAN", c.getString(3));
map.put("ESZKOZ_LEIRAS", c.getString(4));
map.put("ESZKOZ_TIPUS", c.getString(5));
map.put("ESZKOZ_FOCSOPORT_KOD", c.getString(6));
map.put("ESZKOZ_CSOPORT_KOD", c.getString(7));
map.put("ESZKOZ_ALCSOPORT_KOD", c.getString(8));
map.put("ESZKOZ_LELTARCSOP_KOD", c.getString(9));
map.put("ESZKOZ_ALLELTARCSOP_KOD", c.getString(10));
map.put("ESZKOZ_HR_KOD", c.getString(11));
map.put("ESZKOZ_SZERVEZET_KOD", c.getString(12));
map.put("ESZKOZ_MENNYISEG", c.getString(13));
map.put("ESZKOZ_MENNYISEGI_EGYSEG", c.getString(14));
map.put("ESZKOZ_STATUSZ", c.getString(15));
map.put("ESZKOZ_AKTIVALT_ERTEK", c.getString(16));
map.put("ESZKOZ_NETTO_ERTEK", c.getString(17));
map.put("ESZKOZ_SZAMVITEL_KAT", c.getString(18));
map.put("ESZKOZ_MEGJEGYZES", c.getString(19));
map.put("ESZKOZ_BESZERZES_DATUM", c.getString(20));
map.put("ESZKOZ_GARANCIA_KEZD_VEG", c.getString(21));
map.put("ESZKOZ_GYARI_SZAM", c.getString(22));
map.put("ESZKOZ_LELTAR_MENNYISEG", c.getString(23));
map.put("ESZKOZ_LELTAR_ALLAPOT", c.getString(24));
map.put("ESZKOZ_LELTAR_MEGJEGYZES", c.getString(25));
mylist.add(map);
c.moveToNext();
}
c.close();
mSchedule = new SimpleAdapter(context, mylist, R.layout.row_eszkoz,
new String[] { "_id", "ESZKOZ_ID", "ESZKOZ_LELTARSZAM",
"ESZKOZ_EAN", "ESZKOZ_LEIRAS", "ESZKOZ_TIPUS",
"ESZKOZ_FOCSOPORT_KOD", "ESZKOZ_CSOPORT_KOD",
"ESZKOZ_ALCSOPORT_KOD", "ESZKOZ_LELTARCSOP_KOD",
"ESZKOZ_ALLELTARCSOP_KOD", "ESZKOZ_HR_KOD",
"ESZKOZ_SZERVEZET_KOD", "ESZKOZ_MENNYISEG",
"ESZKOZ_MENNYISEGI_EGYSEG", "ESZKOZ_STATUSZ",
"ESZKOZ_AKTIVALT_ERTEK", "ESZKOZ_NETTO_ERTEK",
"ESZKOZ_SZAMVITEL_KAT", "ESZKOZ_MEGJEGYZES",
"ESZKOZ_BESZERZES_DATUM", "ESZKOZ_GARANCIA_KEZD_VEG",
"ESZKOZ_GYARI_SZAM", "ESZKOZ_LELTAR_MENNYISEG",
"ESZKOZ_LELTAR_ALLAPOT", "ESZKOZ_LELTAR_MEGJEGYZES" },
new int[] { R.id.row_eszkoz_id, R.id.row_eszkoz_eszkoz_id,
R.id.row_eszkoz_leltar_szam, R.id.row_eszkoz_ean,
R.id.row_eszkoz_leiras, R.id.row_eszkoz_tipus,
R.id.row_eszkoz_focsoport_kod,
R.id.row_eszkoz_csoport_kod,
R.id.row_eszkoz_alcsoport_kod,
R.id.row_eszkoz_leltarcsoport_kod,
R.id.row_eszkoz_alleltarcsoport_kod,
R.id.row_eszkoz_hr_kod,
R.id.row_eszkoz_szervezeti_egyseg_kod,
R.id.row_eszkoz_mennyiseg,
R.id.row_eszkoz_mennyisegi_egyseg,
R.id.row_eszkoz_statusz,
R.id.row_eszkoz_aktivalt_ertek,
R.id.row_eszkoz_netto_ertek,
R.id.row_eszkoz_szamviteli_kategoria,
R.id.row_eszkoz_megjegyzes,
R.id.row_eszkoz_beszerzes_datuma,
R.id.row_eszkoz_garancia_kezd_veg,
R.id.row_eszkoz_gyari_szam,
R.id.row_eszkoz_leltarozott_allapot,
R.id.row_eszkoz_leltarozott_mennyiseg,
R.id.row_eszkoz_leltarozott_megjegyzes });
list.setAdapter(mSchedule);
}

No comments:

Post a Comment