我想将setListAdapter显示的数据存储到sqlite ..怎么样? 请帮帮我
XML文件http://p-xr.com/xml
package com.pxr.tutorial.xmltest; import java.util.ArrayList; import java.util.HashMap; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class Main extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); ArrayList> mylist = new ArrayList>(); String xml = XMLfunctions.getXML(); Document doc = XMLfunctions.XMLfromString(xml); int numResults = XMLfunctions.numResults(doc); if((numResults map = new HashMap(); Element e = (Element)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(map); } 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(adapter); /* final ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap o = (HashMap) lv.getItemAtPosition(position); Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); } });*/ } }
答案 0 :(得分:2)
我建议你首先解析数据然后将其保存到数据库然后显示。
如果您没有创建数据库,则必须先执行此操作,示例代码如何执行此操作
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主键自动增量”,因为我记得很好,您没有必要,但它会使您的项目更复杂。