Hi, I have a xml file call groups.xml.
I want to read the value "name" and store in array lv_arrr and load it
into an adapter:
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.group_list,
R.id.grouplist, lv_arrr));
The program crash during runtime. What did I do wrong in the code can
anyone help to take a look?
Many thanks.
---/res/xml/groups.xml---
<?xml version="1.0" encoding="utf-8"?>
<!--
This is a mock score XML chunk
-->
<groups>
<group
name="Apple" />
<group
name="Orange" />
<group
name="Pineapple" />
</groups>
---
public class Manual extends Activity {
private ListView lv1;
private String lv_arrr[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manual);
XmlResourceParser allGroups =
getResources().getXml(R.xml.groups);
try {
processGroups(allGroups);
} catch (Exception e) {
// Log.e(DEBUG_TAG, "Failed to load malls", e);
}
lv1=(ListView)findViewById(R.id.myListView);
lv1.setAdapter(new ArrayAdapter<String>(this,
R.layout.group_list, R.id.grouplist, lv_arrr));
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int
position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(Manual.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is =
"+lv1.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
private void processGroups(XmlResourceParser groups) throws
XmlPullParserException,
IOException {
int eventType = -1;
// Find Score records from XML
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
// Get the name of the tag (eg scores or score)
String strName = groups.getName();
if (strName.equals("group")) {
String Name = groups.getAttributeValue(null,
"name");
lv_arrr[eventType] = Name;
}
}
eventType = groups.next();
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en