TL; DR: warum nichtAndroid Java - Document Verwendung zu analysieren() eine res/Layout XML-Datei
File rewardFile = new File("res/layout/reward_cards.xml");
Arbeit im Android Studio?
Ich habe Probleme beim Verweisen auf eine Res/Layout-Datei in Android Studio, um eine DocumentBuilder-Dokument-Instanz zum Parsen zu erhalten.
Was ich versuche, ist eine variable Anzahl von CardView-Objekten am unteren Rand eines RelativeLayout-Objekts zu generieren.
Meine aktuelle Einrichtung umfasst das Aufblasen des CardView-Objekts aus einer separaten Layoutdatei und das Hinzufügen zu einer vorhandenen RelativeLayout-Ansicht. Ändern Sie dann das CardView-XML, um das nächste hinzuzufügende CardView darzustellen, und aktualisieren Sie es erneut aus der Layoutdatei.
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);
}
Und hier ist mein 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>
Also das nächste CardView Zugabe wird schließlich in einer Schleife gesetzt werden, aber ich versuche nur, die
File rewardFile = new File("res/layout/reward_cards.xml");
zu erhalten
Um an dieser Stelle zu arbeiten.
Wie referenziere ich hier die Layoutdatei reward_cards?
Gibt es einen viel einfacheren Weg, um zu erreichen, was ich versuche zu tun?
Wird diese Methode sogar funktionieren?
Dank