How To Implement Nested Xml Structure Into 2 Listviews?
I am trying to parse an XML file with the following structure: ... ... .
Solution 1:
Create a data object Group.
publicclassGroup {
privateString id;
privateString name;
privateString description;
privateArrayList<String> actions;
publicStringgetId() {
return id;
}
publicvoidsetId(String id) {
this.id = id;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicStringgetDescription() {
return description;
}
publicvoidsetDescription(String description) {
this.description = description;
}
publicArrayList<String> getActions() {
return actions;
}
publicvoidsetActions(ArrayList<String> actions) {
this.actions = actions;
}
}
Your activity code will look something like this.
publicclassMainActivityextendsActivityimplementsOnItemClickListener {
private ArrayList<Group> groups;
private ListView list;
private MyAdapter adapter;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.my_list);
list.setAdapter(adapter = newMyAdapter());
list.setOnItemClickListener(this);
parseXML();
}
privatevoidparseXML(){
// TODO: CODE TO PARSE XML
adapter.notifyDataSetChanged();
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
publicclassMyAdapterextendsBaseAdapter{
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn groups.size();
}
@Overridepublic Object getItem(int arg0) {
// TODO Auto-generated method stubreturn groups.get(0);
}
@OverridepubliclonggetItemId(int position) {
// TODO Auto-generated method stubreturn position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
// TODO: Create row view here.// USE groups to create list herereturnnull;
}
}
@OverridepublicvoidonItemClick(AdapterView<?> listView, View rowView, int position, long column) {
// TODO Auto-generated method stubGroupclickedGroup= groups.get(position);
Intentintent=newIntent(this, SecondActivity.class);
Bundlebundle=newBundle();
bundle.putStringArrayList("ACTIONS", clickedGroup.getActions());
intent.putExtra("BUNDLE", bundle);
startActivity(intent);
}
}
Your second activity will look something like this
publicclassSecondActivityextendsActivity {
private ArrayList<String> actions;
private ListView list;
private ActionAdapter adapter;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intentintent= getIntent();
Bundlebundle= intent.getBundleExtra("BUNDLE");
actions = bundle.getStringArrayList("ACTIONS");
list = (ListView)findViewById(R.id.my_list2);
list.setAdapter(adapter = newActionAdapter());
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
publicclassActionAdapterextendsBaseAdapter{
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn actions.size();
}
@Overridepublic Object getItem(int arg0) {
// TODO Auto-generated method stubreturn actions.get(0);
}
@OverridepubliclonggetItemId(int position) {
// TODO Auto-generated method stubreturn position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
// TODO: Create row view here.// USE actions to create list herereturnnull;
}
}
}
Post a Comment for "How To Implement Nested Xml Structure Into 2 Listviews?"