我首先列出我到目前为止的代码......
这是我的主要活动类:
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,它只是两个textViews,用于显示ListView每行中每个项目的名称和类型。
我的main.xml只是一个TextView和一个列表视图。
我真正坚持的是适配器。我之前使用过数据库适配器,通过editTexts和holder对象将行插入数据库,但我从未采用静态数据库,并通过我的listView将其转换为每行查看。任何人都有关于通过listView查看我的数据库需要什么的任何指针?任何帮助将不胜感激,因为我完全被这里难过。在过去的几个小时里,我一直绞尽脑汁试图找出如何做到这一点。谢谢你的帮助!
答案 0 :(得分:0)
SimpleAdapter
适用于静态数据,因此如果要使用动态数据,性能可能会有所不同。如果您想使用动态数据,例如ArrayAdapter
,其他方式可能是使用不同类型的适配器。
答案 1 :(得分:0)
您不需要使用CursorAdapter,简单的ArrayAdapter也可以。只需将您的数据库查询结果存储在ArrayList(您已经在做)中,然后将该列表传递给Adapter的构造函数。您的数组适配器必须能够接受GroceryObj类型的arraylist,然后您可以调用GroceryObj.getName()或getType()来填充ListView