问题
Is there any way to pass a JAXP Node or Document via an Intent and Parcelable? JAXP doesn't implement Parcelable, so the answer is probably --no. Is there any DOM library that implements Parcelable? Can someone provide a working example of that?
Serialization isn't an option; nasty performance hit. Storing the data in res/xml is not an option: it must eventually (by end of project) be encrypted on disk. Android's "compiled" XML Access Tools do not support decrypting the whole XML. Of course I can do it myself with a class.
Here's my starter code that inflates the XML. My goal is to pass a Node or Document from one ListView to another, effectively drilling down the DOM via Lists.
My Document contains information that all activities need to share. Each activity accesses different Nodes, and extracts new information. I've considered exposing the Document via a global, but I don't think it will be safe for multiple Activities to access it that way.
Also, in the working code below I intend to pass a Node to the second ListActivity rather than a String, just haven't gotten that far.
package com.example
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class JAXPListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Document doc = null;
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new File("/sdcard/example.xml"));
} catch (Exception e) {
e.printStackTrace(); //TO-DO: exception handling
}
NodeList nodes = doc.getChildNodes();
String[] nodeList = new String[nodes.getLength()];
for(int i = 0; i<nodes.getLength(); i++) {
Node node = nodes.item(i);
nodeList[i] = nodes.item(i).getNodeName();
}
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item,
R.id.label, nodeList));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String nodeName = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(),
JAXPNodeListActivity.class);
i.putExtra("nodeName", nodeName);
startActivity(i);
}
});
}
}
回答1:
You can simply use a Singleton class. Like:
public class DomObjectManager{
private static DomObjectManager INSTANCE;
private Document doc;
public static DomObjectManager getInstance(){
if(INSTANCE==null){
INSTANCE = new DomObjectManager();
}
return INSTANCE;
}
private DomObjectManager(){
}
//set the shared document;
public void setDocument(Document doc){
this.doc = doc;
}
public void getDocument(){
retunr doc;
}
//call to destroy before when you do not need the singleton any more.
public void destroy(){
INSTANCE == null;
}
}
来源:https://stackoverflow.com/questions/13783528/is-there-any-way-to-pass-a-dom-object-via-an-intent-and-parcelable