2016-10-06 2 views
0

TL; DR:AndroidのメーカーではないのはなぜAndroidのJavaの - (構文解析にはDocumentBuilderを使用した)のres /レイアウトxmlファイル

File rewardFile = new File("res/layout/reward_cards.xml"); 

仕事ですか?

Android Studioでres/layoutファイルを参照して、解析するDocumentBuilder Documentインスタンスを取得する際に問題があります。

私がしようとしているのは、可変量のCardViewオブジェクトをRelativeLayoutオブジェクトの下部に生成することです。

現在の設定では、別のレイアウトファイルからCardViewオブジェクトを膨張させ、既存のRelativeLayoutビューに追加します。その後、追加する次のCardViewを表すようにCardView XMLを変更し、レイアウトファイルから再膨張させます。

private void generateRewards(Context context, RelativeLayout mainRL) { 
    LayoutInflater inflater = LayoutInflater.from(context); 

    View rewardLayout = inflater.inflate(R.layout.reward_cards, null, false); 

    RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rParams.addRule(RelativeLayout.BELOW, mainRL.findViewById(R.id.cardFutureGoals).getId()); //This is the current bottom view 

    mainRL.addView(rewardLayout, rParams); 

    //Now to edit the XML 

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 

    DocumentBuilder docBuilder = null; 
    try { 
     docBuilder = docFactory.newDocumentBuilder(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } 

    File rewardFile = new File("res/layout/reward_cards.xml"); 

    Document rewardDoc = null; 
    if(rewardFile.exists()) { //This Fails 
     try { 
      rewardDoc = docBuilder.parse(rewardFile); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    //Just changing the CardView's id for now 

    Node rootCard = null; 
    rootCard = rewardDoc.getFirstChild(); 
    NamedNodeMap attr = rootCard.getAttributes(); 
    Node rewardId = attr.getNamedItem("id"); 
    rewardId.setTextContent("@+id/reward2"); 

    Transformer t = null; 
    try { 
     t = TransformerFactory.newInstance().newTransformer(); 
    } catch (TransformerConfigurationException e) { 
     e.printStackTrace(); 
    } 

    DOMSource domSource = new DOMSource(rewardDoc); 
    StreamResult s = new StreamResult("res/layout/reward_cards.xml"); //This will probably also fail 
    try { 
     t.transform(domSource, s); 
    } catch (TransformerException e) { 
     e.printStackTrace(); 
    } 

    ... 

    //Inflate again and add the next cardview 

    rewardLayout = inflater.inflate(R.layout.reward_cards, null, false); 

    rParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rParams.addRule(RelativeLayout.BELOW, mainRL.findViewById(R.id.reward1).getId()); 

    mainRL.addView(rewardLayout, rParams); 
} 

そして、ここでは私のreward_cards.xmlです:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/reward1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.30" 
    android:background="#00CF98" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:layout_marginBottom="5dp" 
    android:layout_marginTop="5dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_alignParentLeft="true" 
     android:background="#ffffff"> 

     ... 

    </RelativeLayout> 


</android.support.v7.widget.CardView> 

だから、最終的にループに置かれる次のCardViewを追加することが、私はちょうど

File rewardFile = new File("res/layout/reward_cards.xml"); 
を取得しようとしています

この時点で作業します。

ここでreward_cardsレイアウトファイルを参照するにはどうすればよいですか?

私がやろうとしていることを達成するための方がはるかに簡単ですか?

この方法でも機能しますか?

おかげ

答えて

0

TL; DR:なぜAndroidのメーカーでFile rewardFile = new File("res/layout/reward_cards.xml");仕事はしないのですか?

リソースは開発マシン上のファイルなので、デバイス上のファイルではありません。

次に、追加する次のCardViewを表すようにCardView XMLを変更し、レイアウトファイルから再膨張させます。

実行時にリソースを変更することはできません。

この方法でも機能しますか?

私が何をしようとしている達成するために非常に簡単な方法はありますか?

ステップ1:レイアウトを膨張させます。

ステップ2:CardViewのメソッドを呼び出して内容を変更します。

ステップ3:変更されたCardViewRelativeLayoutに追加します。

ステップ4:すすぎ、リンス、繰り返し。

あなたの代わりにRelativeLayoutRecyclerViewListViewの使用を検討し、そのような多くのCardViewコンテナを持ってしようとしている場合。

関連する問題