korunos
korunos

Reputation: 798

Write to XML-File

I have a special Problem.

I'm loading a XML File from Web with different tags like <job_id>. These XML Data is written to a file named xmlInformations.xml. Every Information is loaded from file to a ArrayList<Hashmap<String,String>> named taskItems

public void setListTaskData(String filename) {

    XMLParser parser = new XMLParser();
    Document doc = parser.getDomElement(readFromFile(filename));

    NodeList nodeListTasks = doc.getElementsByTagName(KEY_TASK);

    for (int i = 0; i < nodeListTasks.getLength(); i++) {
        HashMap<String, String> map = new HashMap<>();
        Element e = (Element) nodeListTasks.item(i);

        map.put(KEY_TASK_UUID_OBJ, parser.getValue(e, KEY_TASK_UUID_OBJ));
        map.put(KEY_TASK_TITLE, parser.getValue(e, KEY_TASK_TITLE));
        map.put(KEY_TASK_INFO, parser.getValue(e, KEY_TASK_INFO));
        map.put(KEY_TASK_OBJECT, parser.getValue(e, KEY_TASK_OBJECT));
        map.put(KEY_TASK_LOCATION, parser.getValue(e, KEY_TASK_LOCATION));
        map.put(KEY_TASK_OBJECT_ID, parser.getValue(e, KEY_TASK_OBJECT_ID));
        map.put(KEY_TASK_OBJECT_SNR, parser.getValue(e, KEY_TASK_OBJECT_SNR));
        map.put(KEY_TASK_REGISTRATION_ID, parser.getValue(e, KEY_TASK_REGISTRATION_ID));
        map.put(KEY_TASK_TASKIMAGE, parser.getValue(e, KEY_TASK_TASKIMAGE));
        map.put(KEY_TASKIMAGE, parser.getValue(e, KEY_TASKIMAGE));
        map.put(KEY_TASK_HEADLINE, parser.getValue(e, KEY_TASK_HEADLINE));
        map.put(KEY_TASK_SUBJECT, parser.getValue(e, KEY_TASK_SUBJECT));
        map.put(KEY_TASK_ACTION, parser.getValue(e, KEY_TASK_ACTION));
        map.put(KEY_TASK_PRIORITY_COLOR, parser.getValue(e, KEY_TASK_PRIORITY_COLOR));
        map.put(KEY_TASK_STATUS, parser.getValue(e, KEY_TASK_STATUS));
        taskItems.add(map);
    }
}

They will displayed on a ListView. After i clicked on an Item, it starts a new Activity named SingleTaskActivity. This Activity gets the Informations by an IntentExtra from TaskListActivity.

It looks like:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    //neue Oberflaeche starten
    Intent in = new Intent(this, SingleTaskList.class);;
    in.putExtra("taskItems", taskItems.get(position));

    startActivity(in);
}

The SingleTaskActivity displays now the Informations.

    super.onCreate(savedInstanceState);

    Intent i = getIntent();
    ListView lv = getListView();
    lv.setOnItemClickListener(this);
    Serializable xmlFileNames = i.getSerializableExtra("xmlFileNames");
    String filename = xmlFileNames.toString()+".kx_task";
    setListTaskData(filename);

    CustomAdapterTasks adapterTasks = new CustomAdapterTasks(this,taskItems);
    setListAdapter(adapterTasks);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

The User now can type something in an Edittext or take a Picture.

And here is the Problem:

When a User finishes his work and is clicking on the button "save" at the bottom, how can i save all these Informations into the File at the same position in that XML File?

I hope it's clear what my Problem is. I'm working now about 12 Days but didn't found out how it works.

Thank you!

King Regards

Upvotes: 1

Views: 49

Answers (1)

Vyacheslav
Vyacheslav

Reputation: 27211

Use startactivityforresult() and @override onactivityresult for the first Activity to save and restore position line in XML file. For instance, read this topic: https://stackoverflow.com/a/10407371/1979882

Upvotes: 1

Related Questions