Skip to content Skip to sidebar Skip to footer

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.

public class Group {
private String id;
private String name;
private String description;
private ArrayList<String> actions;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public ArrayList<String> getActions() {
    return actions;
}
public void setActions(ArrayList<String> actions) {
    this.actions = actions;
}
}

Your activity code will look something like this.

public class MainActivity extends Activity implements OnItemClickListener {

private ArrayList<Group> groups;
private ListView list;
private MyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView)findViewById(R.id.my_list);
    list.setAdapter(adapter = new MyAdapter());
    list.setOnItemClickListener(this);
    parseXML();
}


private void parseXML(){
    // TODO: CODE TO PARSE XML
    adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return groups.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return groups.get(0);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: Create row view here.
        // USE groups to create list here
        return null;
    }

}


@Override
public void onItemClick(AdapterView<?> listView, View rowView, int position, long column) {
    // TODO Auto-generated method stub
    Group clickedGroup = groups.get(position);
    Intent intent = new Intent(this, SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("ACTIONS", clickedGroup.getActions());
    intent.putExtra("BUNDLE", bundle);
    startActivity(intent);
}

}

Your second activity will look something like this

public class SecondActivity extends Activity {

private ArrayList<String> actions;
private ListView list;
private ActionAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("BUNDLE");
    actions = bundle.getStringArrayList("ACTIONS");

    list = (ListView)findViewById(R.id.my_list2);
    list.setAdapter(adapter = new ActionAdapter());
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public class ActionAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return actions.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return actions.get(0);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: Create row view here.
        // USE actions to create list here
        return null;
    }

}

}

Post a Comment for "How To Implement Nested Xml Structure Into 2 Listviews?"