-1

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
  • 295
  • 3
  • 8
  • 1
    So what's actually your problem? Do you want to read your XML file from hard drive or are you wondering why it has to be in assets folder? – Jerry May 16 '15 at 22:03
  • maybe my problem seems too ridicules but its real. I want to link my music.xml file like this : "/assets/music.xml",instead of "http://api.androidhive.info/music/music.xml". – Kiarash Vazirzade May 17 '15 at 05:03
  • Put your file in InputStream like this: InputStream in = getApplicationContext().getAssets().open("music.xml"); When you get it into InputStream, then you can do whatever you want with that data. – Jerry May 17 '15 at 12:50

1 Answers1

0

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.

Community
  • 1
  • 1
Jerry
  • 295
  • 3
  • 8