我将从列出到目前为止的代码开始...
这是我的主要活动课程:
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);
}
}
这是我的 DatabaseHelper 类:
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));
}
}
这是我的 GroceryObj 类:
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;
}
}
我的 DatabaseAdapter 类现在是空白的。这是我需要弄清楚的!:
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;
}
}
然后我有一个 xml 文件,它按名称和类型存储所有杂货:
<?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>
最后,我有一个 row.xml,它只是两个 textView,用于显示 ListView 每一行中每个项目的名称和类型。
我的 main.xml 只是一个 TextView 和一个列表视图。
我真正坚持的是适配器。我以前使用数据库适配器通过editTexts 和持有者对象将行插入数据库,但我从来没有只使用静态数据库并通过我的listView 将其转换为每一行以查看。任何人都可以通过listView查看我的数据库需要什么?任何帮助将不胜感激,因为我在这里完全被难住了。在过去的几个小时里,我一直在绞尽脑汁试图找出如何做到这一点。谢谢你的帮助!