- I am making an application in which database products will be added to an xml file.
- after querying the xml file, these products will be added to an array.
- This array would be sent to the other activity, such as, MainActivity for Menu, but I can't send the array through
Intent
.
//MainActivity
public class MainActivity extends AppCompatActivity {
public ArrayList<Produto> array;
private File Produtos;
private FileOutputStream fileos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array = new ArrayList<Produto>();
//if there is no xml file this goes to the database and adds the products in an xml file
if ((Produtos.exists() == false))
AdicionarProdutosXML();
//query xml file and add the products in an array
ConsultarFicheirosXml();
//pass the array from one activity to another
Intent i=new Intent(MainActivity.this,Menu.class);
i.putExtra("array",array);
startActivity(i);
}
public void ConsultarFicheirosXml()
{
int tipo=0;
String nome="";
float pvp=0;
int quantidade;
int eventType=0;
XmlPullParser parser=null;
File file=null;
FileInputStream fis=null;
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
file = new File("/mnt/sdcard/Produtos.xml");
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parser.setInput(new InputStreamReader(fis));
eventType=parser.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(eventType!=XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if (parser.getName().equalsIgnoreCase("Tipo")) {
Produto p = new Produto();
try {
tipo= Integer.valueOf(parser.nextText());
parser.nextTag();
nome=parser.nextText();
parser.nextTag();
pvp=Float.valueOf(parser.nextText());
parser.nextTag();
quantidade=Integer.valueOf(parser.nextText());
parser.nextTag();
p.tipo=tipo;
p.nome=nome;
p.pvp=pvp;
p.quantidade=quantidade;
array.add(p);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case XmlPullParser.END_TAG:
break;
case XmlPullParser.END_DOCUMENT:
break;
}
try {
eventType = parser.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//another activity
public class Menu extends AppCompatActivity {
public ArrayList<Produto> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Bundle produtos=getIntent().getExtras();
}