Saturday, 3 January 2015

Save important app content into xml resource



I'm completely new to what I'm doing and I actualy think I'm doing it totaly wrong.


Anyway... I was trying to make a test app where you write some String into editText and the application should save it into the next launch.


I tryed to do that through xml resource and it seems completely messed up. Please, help me!


This is how my layout looks like:



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Filedemo" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="130dp"
android:text="finish" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="111dp"
android:ems="10"
android:inputType="textMultiLine" >

<requestFocus />
</EditText>

</RelativeLayout>


This is the xml I'm trying to save the string into:



<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="thetext" type="id"/>
<item name="textik" type="whatever">
<thetext>hello</thetext>
</item>
</resources>


...and finaly my java code:



public class Filedemo extends Activity {
private Document doc;
private EditText etext;
private Button but;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filedemo);

etext = (EditText)findViewById(R.id.editText1);
but = (Button)findViewById(R.id.button1);

Context context = getApplicationContext();
InputStream istream = context.getResources().openRawResource(R.id.thetext);

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
doc = docBuilder.parse(istream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Node textik = doc.getElementsByTagName("thetext").item(1);

etext.setText(textik.getTextContent());

but.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String s = etext.getText().toString();
textik.setTextContent(s);
System.exit(0);
}
});
}

}

No comments:

Post a Comment