我在这个领域是全新的,我的问题很简单,但我找不到任何解决方案。 我试图在Activity中创建ListView。 我试过这个教程:(http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/)。我想用我的数据XML文件替换music.xml。 但我必须交换" http://api.androidhive.info/music/music.xml" [第5行]将我的XML文件放入我的硬盘(在Android项目中)。
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) {
}
});
}
}
例如,我的music.xml位于assets文件夹中。 为什么我不能把它放在res / values中?
答案 0 :(得分:0)
在XMLParser
课程中添加新方法,我们说getXMLFromAssets(String fileName, Context context)
。像这样实现它。
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();
}
这样您就可以以String格式从assets文件夹中获取XML,并且可以照常继续。
我使用了此post中的此方法。