I'll start off by listing the code i have thus far...
This is my main Activity class:
public class SQLiteDatabaseMain extends Activity
{
ListView list;
DatabaseHelper helper;
SQLiteDatabase db;
DatabaseAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
public void init()
{
helper = new DatabaseHelper(this);
list = (ListView) findViewById(R.id.listView1);
db = helper.getWritableDatabase();
helper.populateGrocery(db);
}
}
This is my DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "grocerystoretest.db";
private static final String DATABASE_TABLE = "grocerystoreitems";
private static final String GROCERY_KEY_NAME = "name";
private static final String GROCERY_KEY_TYPE = "type";
private static final int SCHEMA_VERSION = 1;
SQLiteDatabaseMain sqlMain = new SQLiteDatabaseMain();
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE "
+ DATABASE_TABLE
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2)
{
// db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
}
public void insert(String name, String type)
{
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("type", type);
getWritableDatabase().insert(DATABASE_TABLE, "name", cv);
}
public void update(String id, String name, String type)
{
ContentValues cv = new ContentValues();
String[] args =
{ id };
cv.put("name", name);
cv.put("type", type);
getWritableDatabase().update(DATABASE_TABLE, cv, "_ID=?", args);
}
public void delete(String id, String name, String type)
{
ContentValues cv = new ContentValues();
String[] args =
{ id };
cv.put("name", name);
cv.put("type", type);
getWritableDatabase().delete(DATABASE_TABLE, "_ID=?", args);
}
public void populateGrocery(SQLiteDatabase db)
{
ArrayList<GroceryObj> groceryArrayList;
groceryArrayList = buildGroceryArrayList();
String insertStmt = null;
for (int i = 0; i < groceryArrayList.size(); i++)
{
insertStmt = "INSERT INTO " + DATABASE_TABLE + " ("
+ GROCERY_KEY_NAME + ", " + GROCERY_KEY_TYPE + ") "
+ "VALUES (\"" + groceryArrayList.get(i).getName()
+ "\", \"" + groceryArrayList.get(i).getType() + "\");";
db.execSQL(insertStmt);
}
}
private ArrayList<GroceryObj> buildGroceryArrayList()
{
ArrayList<GroceryObj> aL = new ArrayList<GroceryObj>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
Context context;
context = sqlMain.getApplicationContext();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream raw = context.getAssets().open("default_grocerystore.xml");
Document dom = builder.parse(raw);
Element root = (Element) dom.getDocumentElement();
NodeList groceryItems = ((Document) root)
.getElementsByTagName("grocery");
for (int i = 0; i < groceryItems.getLength(); i++)
{
String name = null;
String type = null;
Node item = groceryItems.item(i);
NodeList groceryItem = item.getChildNodes();
for (int j = 0; j < groceryItem.getLength(); j++)
{
Node nodeItem = groceryItem.item(j);
String nodeName = nodeItem.getNodeName();
if (nodeName.equalsIgnoreCase("name"))
{
name = nodeItem.getFirstChild().getNodeValue();
} else if (nodeName.equalsIgnoreCase("type"))
{
type = nodeItem.getFirstChild().getNodeValue();
}
}
aL.add(new GroceryObj(name, type));
}
} catch (Exception e)
{
e.printStackTrace();
}
return aL;
}
public String getName(Cursor c)
{
return (c.getString(1));
}
public String getType(Cursor c)
{
return (c.getString(3));
}
}
This is my GroceryObj class:
public class GroceryObj
{
private String name = "";
private String type = "";
public GroceryObj(String name, String type)
{
this.name = name;
this.type = type;
}
public String getName()
{
return (name);
}
public void setName(String name)
{
this.name = name;
}
public String getType()
{
return (type);
}
public void setType(String type)
{
this.type = type;
}
}
My DatabaseAdapter class is blank right now. This is what I need to figure out!:
public class DatabaseAdapter extends CursorAdapter
{
public DatabaseAdapter(Context context, Cursor c)
{
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
return null;
}
}
Then I have an xml file that stores all of the grocery items by name and type:
<?xml version="1.0" encoding="utf-8"?>
<default_grocerystore>
<grocery>
<name>Bread</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Rolls</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Juice</name>
<type>Beverages</type>
</grocery>
<grocery>
<name>Soda</name>
<type>Beverages</type>
</grocery>
</default_grocerystore>
And finally I have a row.xml that is just two textViews that are to display the name and type of each item in each row of my ListView.
My main.xml is just a TextView and a listview.
What I'm really stuck on is the Adapter. Ive used database adapters before to insert rows into a database via editTexts and a holder object but Ive never just taken a static database and had it converted to each row to View via my listView. Anyone have any pointers on what is needed to get my database viewed via a listView?? Any help would be greatly appreciated because I am completely stumped here. Ive been racking my brain for the past few hours trying to find out how to do this. Thanks for any help!