I'm using an SQLite database in my Android App. I've extrapolated my code from a particular website which I can't seem to locate now. But the code includes a class for stuffing and retrieving a SQLite data row: I call the class dbData. As far as I can tell, it simply stores the data row. There is no constructor, so apparently it creates an instance of the adapter when called using the default. The tostring is returned as dbData. But an Id can be set and gotten too.
My big question is why use this class instead of variables within the other classes that otherwise call this class? My next question is, if the listAdaptor returns the adaptor (per http://ift.tt/1Fwx5a8) what information does it stuff into dbData in the line dbData = (dbData) lstView.getAdapter().getItem(0); that it allows dbData to return a string and grab the correct database row? Is it automatically converting the adapter to the database key field?
Finally, does the line ArrayAdapter adapter = (ArrayAdapter) lstView.getAdapter(); come into play it's not used.
Thanks for any feedback.
Here's the code:
public class dbData {
private long id;
private String dbData;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDbData() {
return dbData;
}
public void setDbData(String dbData) {
this.dbData = dbData;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return dbData;
}
}
Here is an example of how it is called:
public void onClick(View view) {
@SuppressWarnings("unchecked")
ListView lstView = (ListView)findViewById(R.id.lstHolidays);
ArrayAdapter<dbData> adapter = (ArrayAdapter<dbData>) lstView.getAdapter();
dbData dbData = null;
switch (view.getId()) {
case R.id.add:
String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
int nextInt = new Random().nextInt(3);
// save the new dbData to the database
dbData = datasource.createDB_Row(comments[nextInt]);
adapter.add(dbData);
break;
case R.id.delete:
if (lstView.getAdapter().getCount() > 0) {
dbData = (dbData) lstView.getAdapter().getItem(0);
datasource.deleteDB_Row(dbData);
adapter.remove(dbData);
}
break;
}
adapter.notifyDataSetChanged();
}
Aucun commentaire:
Enregistrer un commentaire