我有这个类使用DOMParser从“infofermata.xml”获取数据。这个类叫做ReadXMLFile.java:
/**
* Created by Giacomo B on 30/07/2015.
*/
package com.example.giacomob.myapplication;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import static android.support.v4.app.ActivityCompat.startActivity;
import static java.security.AccessController.getContext;
public class ReadXMLFile {
public static void readXMLFile(Context context) {
try {
// Log.i("MyActivity", "casa");
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("infofermata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
// String filePath = "assets/infofermata.xml";
//File fXmlFile = new File(filePath);
//DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("fermata");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
// Log.i("MyActivity", "casa");
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// System.out.println("Staff id : " + eElement.getAttribute("id"));
// String stringidfermata = "Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent()"";
// Log.i("MyActivity", "\"Id Fermata : \" + eElement.getElementsByTagName(\"idfermata\").item(0).getTextContent()");
System.out.println("Id Fermata : " + eElement.getElementsByTagName("idfermata").item(0).getTextContent());
String testo1 = eElement.getElementsByTagName("idfermata").item(0).getTextContent();
//I would to switch this string in another activiy
System.out.println(testo1); //provo per vedere se stampa quello che ho messo nella variabile "testo1"
System.out.println("Naziome : " + eElement.getElementsByTagName("nazione").item(0).getTextContent());
System.out.println("Paese : " + eElement.getElementsByTagName("paese").item(0).getTextContent());
System.out.println("Via : " + eElement.getElementsByTagName("via").item(0).getTextContent());
}
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我想在另一个叫做“acivity_page1.xml”的活动中切换字符串“testo1”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="182dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ciaoooo"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.29" />
</LinearLayout>
由“Page1.java”调用
package com.example.giacomob.myapplication;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Giacomo B on 05/08/2015.
*/
public class Page1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
}
}
MainActivity是:
package com.example.giacomob.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadXMLFile.readXMLFile(this);
Button b_load=(Button)findViewById(R.id.button_send);
b_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent openPage1 = new Intent(MainActivity.this, Page1.class);
startActivity(openPage1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如何在activity_page1中的视频中显示此字符串?拜托,帮帮我..谢谢