0
您好我設計了一個文件資源管理器,用於顯示一些XML文件,並根據所選的一個打開一個XML或另一個XML文件。直到完全沒有問題爲止,在XMLPullParser的幫助下,我將屬性中的文本插入到某些ArrayLists中。問題是,如果我返回並選擇另一個XML文件,則這些值與以下XML相同,並且發生在以下XML中。我認爲問題在於,一旦新實例被調用,arrayLists就不會變空。Android:ParsingXMLFiles只獲取第一個XML的值
這是如何工作的: FileExporer我在哪裏顯示文件,我點擊任何人。
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
static File file;
static String texto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead() && (file.getName().endsWith("xml"))){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
file = new File(path.get(position));
file.getName();
XMLPullParser.Parse(this);
texto = XMLPullParserHandler.getA(0);
}
Parseo();
Intent i = new Intent(MainActivity.this, Correccion.class);
startActivity(i);
MainActivity.this.finish();
}
public void Parseo(){
}
public static String fileName(){
return file.getName();
}
public static String getText(){
return texto;
}
}
XMLPullParser:它表示需要讀取的文件並調用一個新的XMLPullParserHandler實例。
public class XMLPullParser{
static String Fichero;
public static void Parse(Context context){
try {
List<Puntuacion> puntuacion;
XMLPullParserHandler parser = new XMLPullParserHandler();
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, MainActivity.fileName());
FileInputStream iStream = new FileInputStream(yourFile);
puntuacion = parser.parse(iStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
XMLPullParserHandler:我認爲現在的問題是,當我創建並添加文本的ArrayList:在的ArrayList例如,在這種情況下,
public class XMLPullParserHandler {
List<Puntuacion> puntuaciones;
private Puntuacion puntuacion;
static List<String> nombres = new ArrayList<String>();
static List<String> a = new ArrayList<String>();
private String text;
public XMLPullParserHandler() {
// puntuaciones.clear();
// puntuaciones.removeAll(puntuaciones);
puntuaciones = new ArrayList<Puntuacion>();
;
}
public List<Puntuacion> getPuntuacion() {
return puntuaciones;
}
public List<Puntuacion> parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("TEST")) {
// create a new instance of puntuacion
puntuacion = new Puntuacion();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("TEST")) {
puntuaciones.add(puntuacion);
} else if (tagname.equalsIgnoreCase("NUMERO_ACIERTOS")) {
puntuacion.setValor_Transformado((text));
a.add(text);
Log.i("ii", text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return puntuaciones;
}
public static String getNombre(int posicion){
String[] ListN = new String[nombres.size()];
ListN = nombres.toArray(ListN);
return ListN[posicion];
}
public static String getA(int posicion){
String[] ListA = new String[a.size()];
ListA = a.toArray(ListA);
return ListA[posicion];
}
}
For example if I have in this case a XML with **<NUMERO_ACIERTOS>1</NUMERO_ACIERTOS>** and after this I read another one with **<NUMERO_ACIERTOS>3</NUMERO_ACIERTOS>** in my UI I only see the value **1** because is the first that has been loaded into the arraylist.
感謝您的時間,注意。
哇!我知道這是由於一個愚蠢的錯誤。格拉西亞斯海爾! – Katherine99 2013-04-12 07:36:17