看看这段代码会对你有帮助
包 com.pxr.tutorial.xmltest;
导入 java.util.ArrayList;
导入 java.util.HashMap;
导入 org.w3c.dom.Document;
导入 org.w3c.dom.Element;
导入 org.w3c.dom.NodeList;
导入android.app.ListActivity;
导入 android.content.ContentValues;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.util.Log;
导入 android.widget.ListAdapter;
导入 android.widget.SimpleAdapter;
导入 android.widget.TextView;
导入 android.widget.Toast;
公共类 Main 扩展 ListActivity {
//private static final String TAG = "com.pxr.tutorial.xmltest.Main";
私有 SQLiteDatabase 数据库;
//静态最终字符串 ID="id";
静态最终字符串 NAME="name";
静态最终字符串 SCORE="score";
文本视图错误;
// 再也不要这样做了!!!
// public void onCreate(SQLiteDatabase db) {
// db.execSQL("CREATE TABLE 常量 (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, score INTEGER);");
//
// 内容值 cv=new ContentValues();
//
// //测试数据
// cv.put(NAME, "Ajay");
// cv.put(SCORE, "100");
// db.insert("常量", null, cv);
//
// }
/** 在第一次创建活动时调用。 */
@覆盖
公共无效 onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
//如果数据库不存在它正在打开数据库它正在创建它
数据库 = (new DatabaseHelper(this).getWritableDatabase());
//使用字段和访问器更好地创建自己的类
ArrayList> mylist = new ArrayList>();
字符串 xml = XMLfunctions.getXML();
文档 doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults map = new HashMap();
元素 e = (元素)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", XMLfunctions.getValue(e, "name"));
map.put("分数", XMLfunctions.getValue(e, "分数"));
mylist.add(地图);
}
//阅读一些关于 JAVA 中的迭代器和集合的信息
for (int j = 0; j parent, View view, int position, long id) {
@SuppressWarnings("未选中")
HashMap o = (HashMap) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' 被点击。", Toast.LENGTH_LONG).show();
}
});*/
}
私人无效进程添加(字符串字符串,字符串string2,字符串string3){
// TODO 自动生成的方法存根
ContentValues 值 = 新 ContentValues(2);
// values.put("id", string);
values.put(DatabaseHelper.NAME, string2);
values.put(DatabaseHelper.SCORE, string3);
如果(数据库!=空){
//如果数据库打开则插入
database.insert(DatabaseHelper.TABLE_NAME, null, values);
// Log.i(TAG, "保存在数据库中");
}别的{
//如果数据库关闭比插入先打开
数据库 = (new DatabaseHelper(this).getWritableDatabase());
//Log.e(TAG, "数据库关闭,打开...");
database.insert(DatabaseHelper.TABLE_NAME, null, values);
//Log.i(TAG, "插入数据库");
}
}
@覆盖
受保护的无效 onDestroy() {
//如果您正在完成应用程序,请始终关闭数据库
如果(数据库!=空){
数据库.close();
}
super.onDestroy();
}
}