how can i link an XML data file from my hard drive (not web) to my project?

Kiarash Vazirzade

I am completely new in this field, my problem is simple but I couldn't find any solution. I am trying to create ListView in Activity. I tried this tutorial: (http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/). I want to replace the music.xml with my data XML file. But I have to exchange the "http://api.androidhive.info/music/music.xml" [in line 5] with my XML file into my hard drive (in Android project foder).

package com.example.androidhive;


import android...;

public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

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


        }
    });     
}   
}

For example my music.xml is in assets folder. And why can't I put it in res/values?

Jerry

Add new method to your XMLParser class, let's say getXMLFromAssets(String fileName, Context context). Implement it like this.

public String getXMLFromAssets(String fileName, Context context) {

    StringBuilder returnString = new StringBuilder();
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader input = null;

    try {
        inputStream = context.getResources().getAssets()
                .open(fileName, Context.MODE_WORLD_READABLE); // Context.MODE_WORLD_READALE is maybe not necessary
        inputStreamReader = new InputStreamReader(inputStream);
        input = new BufferedReader(inputStreamReader);
        String line = "";

        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }

    } catch (Exception e) {
        e.getMessage();
    } finally {
        try {
            if (inputStreamReader != null)
                inputStreamReader.close();
            if (inputStream != null)
                inputStream.close();
            if (input != null)
                input.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }
    return returnString.toString();
}

This way you obtain your XML from assets folder in String format and you can carry on as usual.

I used this method from this post.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

分類Dev

My External Hard Drive fell and I cannot access storage anymore. How can I fix it?

分類Dev

Is there any way I can fix my corrupted Hard Drive?

分類Dev

How can I view what percentage of my hard drive bandwidth is currently being used?

分類Dev

How to recovery the data inside my hard drive? New to Ubuntu

分類Dev

how do i fix my read only hard drive?

分類Dev

I can't link my JQuery file to my HTML file

分類Dev

How can i create a custom ListView with my xml layout file?

分類Dev

Is my hard drive dying?

分類Dev

How to transfer a DVD to my hard drive

分類Dev

How to transfer a DVD to my hard drive

分類Dev

How can I boot Windows at external hard drive from internal hard drive?

分類Dev

How can i load a link in other net browser that i am showing in my web view?

分類Dev

How can I find out which program creates a file/folder in my C: drive?

分類Dev

How can I put data from my datagridview to textboxes?

分類Dev

What are some activities I can do to measure the top performance of my hard drive?

分類Dev

How to open my external hard drive, which is locked by password.exe file in ubuntu?

分類Dev

How can I copy files from my internal drive to an external USB 3.0 drive if Windows doesn't boot up?

分類Dev

How much "Load_cycle_count" can my hard drive hypotethically sustain?

分類Dev

how do i get my site web icon from direct link

分類Dev

How can I mount a drive under my home directory at boot?

分類Dev

How do I run chkdsk to repair my laptop's hard drive?

分類Dev

How do I get a complete list of files in my hard drive in a convenient format?

分類Dev

How i can get the td data getting from css to attach to my current row ,and use for my logic

分類Dev

Opening csv files from my hard drive and importing into a database

分類Dev

How to check the disk activity of my hard-drive?

分類Dev

How do I suppress the method name from my xml in a WCF Web Service?

分類Dev

How can I sent an activation link to my registered user in Laravel?

分類Dev

how i can test my data annotation field in core web api?

Related 関連記事

  1. 1

    How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

  2. 2

    My External Hard Drive fell and I cannot access storage anymore. How can I fix it?

  3. 3

    Is there any way I can fix my corrupted Hard Drive?

  4. 4

    How can I view what percentage of my hard drive bandwidth is currently being used?

  5. 5

    How to recovery the data inside my hard drive? New to Ubuntu

  6. 6

    how do i fix my read only hard drive?

  7. 7

    I can't link my JQuery file to my HTML file

  8. 8

    How can i create a custom ListView with my xml layout file?

  9. 9

    Is my hard drive dying?

  10. 10

    How to transfer a DVD to my hard drive

  11. 11

    How to transfer a DVD to my hard drive

  12. 12

    How can I boot Windows at external hard drive from internal hard drive?

  13. 13

    How can i load a link in other net browser that i am showing in my web view?

  14. 14

    How can I find out which program creates a file/folder in my C: drive?

  15. 15

    How can I put data from my datagridview to textboxes?

  16. 16

    What are some activities I can do to measure the top performance of my hard drive?

  17. 17

    How to open my external hard drive, which is locked by password.exe file in ubuntu?

  18. 18

    How can I copy files from my internal drive to an external USB 3.0 drive if Windows doesn't boot up?

  19. 19

    How much "Load_cycle_count" can my hard drive hypotethically sustain?

  20. 20

    how do i get my site web icon from direct link

  21. 21

    How can I mount a drive under my home directory at boot?

  22. 22

    How do I run chkdsk to repair my laptop's hard drive?

  23. 23

    How do I get a complete list of files in my hard drive in a convenient format?

  24. 24

    How i can get the td data getting from css to attach to my current row ,and use for my logic

  25. 25

    Opening csv files from my hard drive and importing into a database

  26. 26

    How to check the disk activity of my hard-drive?

  27. 27

    How do I suppress the method name from my xml in a WCF Web Service?

  28. 28

    How can I sent an activation link to my registered user in Laravel?

  29. 29

    how i can test my data annotation field in core web api?

ホットタグ

アーカイブ