2

サーバーからのグリッドビューにデータを表示したいのですが、サーバー上にデータを含むxmlファイルがあります。http://developer.android.com/guide/tutorials/views/hello-gridview.htmlリンクからグリッドビューにデータを表示しようとしています。

しかし、私はサーバーからそれをどのように行うことができるかを示したいと思います。これは私にとって新しいことなので、サンプルコードやリンクはありがたいです。

4

2 に答える 2

0

このコードを試して、

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();
            }

        }

    }

このコードでは、DOM 解析を使用しました。それを自分で変更します。

于 2012-04-24T08:26:19.233 に答える
0

アダプターをグリッドビューに設定します。(つまり、ArrayAdapter) doInbackground 関数でサーバーから情報をダウンロードする AsyncTask を記述します。

AsyncTask の onPostExecute 関数でアダプタの内容を更新します

チェック: AsyncTask のhttp://developer.android.com/reference/android/os/AsyncTask.html、例もあります

于 2012-04-24T08:21:33.870 に答える