gpt4 book ai didi

android - 使用 XML 填充 SQLite 并集成到 Activity 中

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:56 24 4
gpt4 key购买 nike

在我找到的所有教程(包括 Stackoverflow 中的答案)中,我找不到一个完整 教程来展示如何实现 XML 到 SQLite 的填充。我创建了一个数据库助手,但我不知道如何将它集成到我的 Activity 中。我想做的是;一旦应用程序启动,它就应该用 XML 填充数据库。

XML

我在“res\xml”文件夹中有一个 XML 文件“amawal_posts.xml”,其中包含一些我想填充到数据库中的条目。

<?xml version="1.0" encoding="utf-8"?>
<database name="npma_amawal" >
<!-- Table wp_posts -->
<table name="wp_posts" >
<column name="ID" >948</column>
<column name="post_content" >اورغ</column>
<column name="post_title" >ure</column>
</table>
<table name="wp_posts" >
<column name="ID" >46</column>
<column name="post_content" >adlis g llan iwaliwn FR: dictionnaire.</column>
<column name="post_title" >amawal</column>
</table>
</database>

此 XML 包含超过 4000 条记录。

SQLiteOpenHelper

这是“XMLtoSQLite.java”的内容

package com.np.amawalandroiddb;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import com.np.amawalandroiddb.R;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class XMLtoSQLite extends SQLiteOpenHelper {

private final Context fContext;

// Set TAG for error catching
public static String TAG = "XMLtoSQLite";

// Set database columns
public static String column_ID = null;
public static String column_post_content = null;
public static String column_post_title = null;

public XMLtoSQLite(Context context) {
super(context, "amawal", null, 1);
fContext = context;
}


public void createDataBase (SQLiteDatabase db) throws IOException {
db.execSQL("CREATE TABLE amawal_posts (" + "ID INTEGER PRIMARY KEY,"
+ "post_content TEXT," + "post_content TEXT" + ");");

// Add default records amawal_posts
ContentValues Columns = new ContentValues();

// Get XML resource file
Resources res = fContext.getResources();

// Open XML file
int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
XmlResourceParser database = res.getXml(R.xml.amawal_posts);
String name = database.getText();
Log.d(TAG, name);

try {
if (database.getEventType() == XmlResourceParser.START_TAG)
{
String s = database.getName();

if (s.equals("table"))
{
database.next(); // moving to the next node
if (database.getName() != null && database.getName().equalsIgnoreCase ( "column"))
{
column_ID = database.getText(); // to get value getText() method should be used
database.next();

column_post_content = database.getText();
database.next();

column_post_title = database.getText();

// Insert the values inside the DB
Columns.put("ID", column_ID);
Columns.put("post_content", column_post_content);
Columns.put("post_title", column_post_title);

db.insert("amawal", null, Columns);

}

Log.d(TAG, column_ID);
Log.d(TAG, column_post_content);
Log.d(TAG, column_post_title);
}
}
}
//Catch errors
catch (XmlPullParserException e)
{
Log.e(TAG, e.getMessage(), e);
}
catch (IOException e)
{
Log.e(TAG, e.getMessage(), e);

}
finally
{
//Close the XML file
database.close();
}
}
}

/* Update database to latest version */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Crude update, make sure to implement a correct one when needed.

Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS animals");
onCreate(db);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

}

我的主要 Activity

“MainActivity.java”的内容

package com.np.amawalandroiddb;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Begin DB work
XMLtoSQLite db = new XMLtoSQLite(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

作为 Java 编程的初学者, Activity/布局/服务之间的关系..有点难以理解:(

问题:如何在我的 Activity 中调用/执行它?

最佳答案

createDataBase() 方法应该在 XMLtoSQLite 类的 onCreate() 方法中调用

关于android - 使用 XML 填充 SQLite 并集成到 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com