\In android programming..i like to retrieve data's in the xmltags from url(xmlparsing) in the meantime i want to load progress until the data retrieve from xmltags. i attach my code check whether its rite or wrong thank you\
\\main activity\\
public class act extends Activity
{
\\button used to start the function after the click\\
Button b;
public ProgressDialog mDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new GetTask().execute();
}
});
}
\\my asynctask\\
class GetTask extends AsyncTask<Object, Void, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
mDialog = new ProgressDialog(act.this);
mDialog.setMessage("Please wait...");
mDialog.show();
}
@Override
protected String doInBackground(Object... params)
{
\\ui programetically declare the textviews\\
try {
LinearLayout layout = new LinearLayout(act.this);
layout.setOrientation(1);
TextView no[];
TextView na[];
TextView c[];
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
no = new TextView[nodeList.getLength()];
na = new TextView[nodeList.getLength()];
c = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
no[i] = new TextView(act.this);
na[i] = new TextView(act.this);
c[i] = new TextView(act.this);
Element fstElmnt = (Element) node;
NodeList idlist = fstElmnt.getElementsByTagName("id");
Element numelement = (Element) idlist.item(0);
idlist = numelement.getChildNodes();
no[i].setText("ID="+ ((Node) idlist.item(0)).getNodeValue());
NodeList namelist = fstElmnt.getElementsByTagName("name");
Element namelement = (Element) namelist.item(0);
namelist = namelement.getChildNodes();
na[i].setText("pizza name="+ ((Node) namelist.item(0)).getNodeValue());
NodeList costlist = fstElmnt.getElementsByTagName("cost");
Element costlement = (Element) costlist.item(0);
costlist = costlement.getChildNodes();
c[i].setText("cost="+ ((Node) costlist.item(0)).getNodeValue());
layout.addView(no[i]);
layout.addView(na[i]);
layout.addView(c[i]);
setContentView(layout);
}}
catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
mDialog.dismiss();
}
}
}
The Following code is Wrong.
U dont have any data in your onPostExecute() so whatever youe want to display just show that thing in onPostExecute() instead of doInBackground().
So change it ..
A sample example u can find in LazyLoading Or Asynctask App:-
May with following code u can get ur ans...!!!!
First u cannot make the UI related actions in the doInBackground()
If u want u can do it in the onPostExecute() after completed the doInBackground() process ...
So change ur logic ..