将解析的 xml 数据存储到 sqlite ?安卓
Posted
技术标签:
【中文标题】将解析的 xml 数据存储到 sqlite ?安卓【英文标题】:Store parsed xml data to sqlite ? Android 【发布时间】:2011-08-07 20:18:46 【问题描述】:我想将 setListAdapter 显示的数据存储到 sqlite .. 我该怎么办?? 请帮帮我
XML 文件http://p-xr.com/xml
包 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.os.Bundle; 导入android.view.View; 导入 android.widget.AdapterView; 导入 android.widget.AdapterView.OnItemClickListener; 导入 android.widget.ListAdapter; 导入 android.widget.ListView; 导入 android.widget.SimpleAdapter; 导入 android.widget.Toast; 公共类 Main 扩展 ListActivity /** 在第一次创建活动时调用。 */ @覆盖 公共无效 onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); 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", "Naam:" + XMLfunctions.getValue(e, "name")); map.put("Score", "Score: " + XMLfunctions.getValue(e, "score")); mylist.add(地图); ListAdapter 适配器 = new SimpleAdapter(this, mylist, R.layout.main, 新字符串[] "id","name", "Score" , 新的 int[] R.id.id, R.id.item_title, R.id.item_subtitle ); setListAdapter(适配器); /* 最终列表视图 lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() public void onItemClick(AdapterView 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(); );*/【问题讨论】:
【参考方案1】:我建议你先解析数据然后存入数据库再显示。
如果您没有创建数据库,则必须先创建,示例代码如下所示
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
*
* @author Robert
*
*/
public class DatabaseHelper extends SQLiteOpenHelper
/**
* Default constructor- creates database
* @param context
*/
public DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, 1);
/**
* Implemented methood from SQLiteOpenHelper- called when
* constructor of DatabaseHelper is called
* Creates three tables (TRIP, POINT, TRANSFER) with relations
* TRAVEL-POINT and POINT TRANSFER
* @see SQLiteOpenHelper
*/
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE " + TRIP_TABLE_NAME
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ FROM + " TEXT, "
+ TO + " TEXT);");
db.execSQL("CREATE TABLE " + TRANSFER_TABLE_NAME
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NOTE + " TEXT, "
+ TIME + " TEXT, "
+ DATE + " TEXT, "
+ ADDRESS + " TEXT, "
+ CITY + " TEXT, "
+ COUNTRY + " TEXT, "
+ LATITUDE + " TEXT, "
+ LOGITUDE + " TEXT, "
+ TRANFER_TYPE + " TEXT, "
+ FK_POINT + " INTEGER NOT NULL REFERENCES "+ POINT_TABLE_NAME +"(_id)"
+ "ON DELETE CASCADE);");
db.execSQL("CREATE TABLE " + POINT_TABLE_NAME
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " TEXT, "
+ ADDRESS + " TEXT, "
+ CITY + " TEXT, "
+ COUNTRY + " TEXT, "
+ LATITUDE + " TEXT, "
+ LOGITUDE + " TEXT, "
+ FK_TRAVEL + " INTEGER NOT NULL REFERENCES "+ TRIP_TABLE_NAME +"(_id)"
+ "ON DELETE CASCADE);");
/**
* Implemented method from SQLiteOpenHelper- we don't use it now
* @see SQLiteOpenHelper
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
/**
* Private fields, mainly names of columns and tables in database
*/
private static final String DATABASE_NAME = "TRAVELER_NOTEBOOK";
//Table names
private static final String TRIP_TABLE_NAME = "TRIP";
private static final String POINT_TABLE_NAME = "POINT";
private static final String TRANSFER_TABLE_NAME = "TRANSFER";
//Columns names
private static final String FROM = "FROM_PLACE";
private static final String TO = "TO_PLACE";
private static final String NAME = "NAME";
private static final String ADDRESS = "ADDRESS";
private static final String CITY = "CITY";
private static final String COUNTRY = "COUNTRY";
private static final String LATITUDE = "LATITUDE";
private static final String LOGITUDE = "LOGITUDE";
private static final String NOTE = "NOTE";
private static final String TIME = "TIME";
private static final String DATE = "DATE";
private static final String TRANFER_TYPE = "TRANSFER_TYPE";
//Foregin keys name
private static final String FK_TRAVEL = "FK_TRAVEL_ID";
private static final String FK_POINT = "FK_POINT_ID";
下一步是在你的活动中获取可写数据库,你可以通过创建来做到这一点
SQLiteDatabase database = (new DatabaseHelper(this).getWritableDatabase());
其中 DatabaseHelper 是您的数据库助手类的名称。如果你想在数据库中插入一些东西,就像下面这样:
private void processAdd(values_to_save)
ContentValues values = new ContentValues(number_of_values);
values.put(column_name, value);
values.put(column_name, value);
if(database!=null)
database.insert(TABLE_NAME, null, values);
else
Log.e("MyAppError", "Database is null");
ContentValue 类似于 HashMap 键值,其中键是列名,值是您要插入的内容。 请记住,在每个表中,您应该有列“_id 主键自动增量”,因为我记得很好,您不必这样做,但这会使您的项目更加复杂。
【讨论】:
谢谢 robert ... 现在我知道如何创建数据库了,.... 但是我如何将解析的 xml 数据插入 sqlite 数据库...ListAdapter adapter = new SimpleAdapter(this , mylist , R.layout.main, new String[] "id","name", "Score" , new int[] R.id.id, R.id.item_title, R.id.item_subtitle ); setListAdapter(适配器);看到这段代码... 好的,你已经解析了 xml 并构建了 listview,对吗?并告诉我你需要把所有这些元素或选择一个? 是的,正确...是的,我解析它并生成列表视图...现在我想将所有元素存储在sqlite中...请帮助.. 在我看来,最好先存储它,而不是用元素生成视图;)。如我所见,您有一个带有 hashmap 的列表,因此制作了带有 3 列表的数据库,例如 for(i = 0; i private void processAdd(String str1, String str2, String str3) ContentValues values = new ContentValues(3); values.put(column_name, str1); values.put(column_name, str2);values.put(column_name, str3) if(database!=null) database.insert(TABLE_NAME, null, values); else Log.e("MyAppError", "Database is null"); 最好把代码发给我
以上是关于将解析的 xml 数据存储到 sqlite ?安卓的主要内容,如果未能解决你的问题,请参考以下文章