2

I want to show the data in a gridview from the server and I have the xml file on the server which contain the the data. I have study about to show the data on Grid View from http://developer.android.com/guide/tutorials/views/hello-gridview.html link.

But I want to show it from server how can I do this. any sample code or link is appreciated because this is a new thing for me.

BraveGirl
  • 75
  • 6
  • How are you going to get data from server, either by using SOAP or accessing direct URLs? – Harpreet Apr 26 '12 at 10:10
  • And do check this http://stackoverflow.com/questions/9414190/to-use-progressdialog-till-gridview-gets-loaded-from-webservice Hope if it will help. – Harpreet Apr 26 '12 at 10:13
  • @Haps my xml file is on the server from which I want to fetch the wallpaper into the Grid View and Songs url is in another xml which I want to use – BraveGirl Apr 26 '12 at 10:38
  • So wat have you coded till now? – Harpreet Apr 27 '12 at 04:43

2 Answers2

0

set an adapter to your gridview. (i.e. ArrayAdapter) write an AsyncTask that downloads the information from server in doInbackground function,

update the adapter contents in onPostExecute function of AsyncTask

Check: http://developer.android.com/reference/android/os/AsyncTask.html for AsyncTask, there is also an example

Siyamed
  • 495
  • 2
  • 11
0

Try this code,

public class Get_User_Data extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                GalleryShow.this);

        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            URL url = null;
            try {
                url = new URL("<Put your link here>");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = null;
            try {
                db = dbf.newDocumentBuilder();
            } catch (ParserConfigurationException e1) {
                e1.printStackTrace();

            }
            Document doc = null;
            try {
                doc = db.parse(new InputSource(url.openStream()));
            } catch (SAXException e2) {

                e2.printStackTrace();
            } catch (IOException e3) {

                e3.printStackTrace();
            }
            org.w3c.dom.Element elt;
            try {
                elt = doc.getDocumentElement();
                NodeList nodeList = elt.getElementsByTagName("file");
                temp = new String[nodeList.getLength()];

                for (int i = 0; i < nodeList.getLength(); i++) {

                    Element pathelement = (Element) nodeList.item(i);
                    imgList.add(pathelement.getAttribute("path"));

                    System.out.println("Images List"
                            + pathelement.getAttribute("path"));
                    list_data.add(new List_Data(pathelement
                            .getAttribute("path"), i + ""));
                }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dealAdapter = new LazyAdapter(GalleryShow.this,
                    R.id.ImageView01, list_data);
            return null;
        }

        protected void onPostExecute(Void result) {

            gridview.setAdapter(dealAdapter);
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }

        }

    }

In this code i used DOM parsing, you will modify it yours.

Aerrow
  • 12,086
  • 10
  • 56
  • 90