0

私はAndroidアプリの開発に不慣れです。私がやりたいのは、データを取得して配列に格納し、そのデータをリストアクティビティビューに表示することです。ある意味で、アプリは解析されたツリーに似ており、リスト内の1つのアイテムをクリックすると、別のアイテムに移動します。重要なのは、複数の異なるリストを作成し、ほとんどが何らかの形で接続されることです(事前に選択したカテゴリによって異なります)。私はMapsDemoサンプルアプリケーションを調べていて、方法があることを知っていますが、まだ理解していません。これがまったく混乱している場合は、私に知らせてください...

皆さんありがとう

4

1 に答える 1

0

手始めにこれを試してみてください。これができるようになったら、動的データの使用に移ります。私はこれを非常によく似たプロジェクトに使用し、うまく機能しました:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

これが私が使用したコードです。多分これはあなたを助けるでしょう:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dining);
        //Hard coded array
        ListData newListItem = new ListData();
        ArrayList<ListData> itemObject=null;
        try {

             itemObject= newListItem.getData(DATAURL);
             Log.e(TAG, "item object: "+itemObject.toString());
             ListAdapter adapter = new ListAdapter(this, R.layout.dininglistview, itemObject);
             //Create listview
             this.listView1 = (ListView)this.findViewById(android.R.id.list);
             this.listView1.setAdapter(adapter);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            Log.e(TAG, "Unexpected error", e);
        } catch (ServiceException e){
            AlertDialog alertDialog = new AlertDialog.Builder(DiningActivity.this).create();
            alertDialog.setTitle("Temporarily unavailable");
            alertDialog.setMessage("Please contact top25@uievolution.com");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Intent homeIntent = new Intent(DiningActivity.this, HomeActivity.class);
                    DiningActivity.this.startActivity(homeIntent);

                }
            });
            alertDialog.show();
        }
        //implement dining adapter

    }

XMLの解析に使用したgetData()メソッドは次のとおりです。

public ArrayList<ListData> getData(String DATAURL) throws InterruptedException, ServiceException {

        ArrayList<ListData> items=null;


        HttpURLConnection conn = null;
        try {
            URL url = new URL(DATAURL);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(TIMEOUT);
            conn.setConnectTimeout(CONNECT_TIMEOUT);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            if(Thread.interrupted())
            {
                throw new InterruptedException();
            }


             items = parseXML(conn);



        } catch (MalformedURLException e) {

            Log.e(TAG, "Invalid URL", e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAG, "Unable to connect to URL", e);
        }
        finally{
            if(conn!=null){
                conn.disconnect();
            }
        }

        return items;

    }

これが私のパーサーです:

private static ArrayList<ListData> parseXML(HttpURLConnection conn) throws ServiceException {
    ArrayList<ListData> dataArray = new ArrayList<ListData>();


    DocumentBuilderFactory builderFactory =
            DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    try {

        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        Log.e(TAG, "Parse Configuration issue", e);
        throw new ServiceException("Service Exception Error");
    } catch (IllegalAccessError e){
        Log.e(TAG, "Illegal Accessor Error", e);
        throw new ServiceException("Service Exception Error");
    }

    try {
        //parse input from server
        Document document = builder.parse(conn.getInputStream());
        Element xmlElement  = document.getDocumentElement();
        NodeList recordNodes = xmlElement.getChildNodes();

        //assign parsed data to listItem object
        for(int i=0; i<recordNodes.getLength(); i++){

          Node record = recordNodes.item(i);
          NodeList recordDetails = record.getChildNodes();
          ListData listItem = new ListData();

          for(int ii=0; ii<recordDetails.getLength(); ii++){
             Node detailItem = recordDetails.item(ii);
             String detailType = detailItem.getNodeName();
             String detailValue = detailItem.getTextContent();

             //assign attributes to listItem object
             if(detailType.matches("item")){
                 int itemValue = Integer.parseInt(detailValue);
                 listItem.setItem(itemValue);
             } else if(detailType.matches("title")){
                 listItem.setTitle(detailValue);
             } else if(detailType.matches("subhead")){
                 listItem.setSubhead(detailValue);

             } else if(detailType.matches("thumb")){
                 ImageManager im = new ImageManager();
                 Bitmap tImg = im.getImage(detailValue);
                 listItem.setThumb(tImg);
             } else if(detailType.matches("photo")){
                 ImageManager im = new ImageManager();
                 Bitmap pImg = im.getImage(detailValue);
                 listItem.setPhoto(pImg);
             } else if(detailType.matches("localAddress")){
                 listItem.setLocalAddress(detailValue);
             } else if(detailType.matches("phone")){
                 listItem.setPhone(detailValue);
             } else if(detailType.matches("webUrl")){
                 URL webUrl1 = new URL(detailValue);
                 listItem.setWebURL(webUrl1);
             } else if(detailType.matches("facebook")){
                 listItem.setFacebook(detailValue);
             } else if(detailType.matches("twitter")){
                 listItem.setTwitter(detailValue);
             } else if(detailType.matches("latitude")){
                 float itemLat = Float.parseFloat(detailValue);
                 listItem.setLatitude(itemLat);
             } else if(detailType.matches("longitude")){
                 float itemLon = Float.parseFloat(detailValue);
                 listItem.setLatitude(itemLon);
             } else if(detailType.matches("notes")){
                 listItem.setNotes(detailValue);
             } else if(detailType.matches("comments")){
                 listItem.setComments(detailValue);
             }

          }
          dataArray.add(listItem);

        }

    } catch (SAXException e) {
        //TODO
        e.printStackTrace();
    } catch (IOException e) {
        //TODO
        e.printStackTrace();
    }

    return dataArray;
}
于 2012-07-02T21:07:34.453 に答える