MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
ListView list;
MyAdapter listadapter;
ArrayList<String> names=new ArrayList<String>();
ArrayList<String> addresses=new ArrayList<String>();
DocumentBuilderFactory dbf;
DocumentBuilder db;
Document doc;
String value;
Context cxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
list=(ListView) findViewById(R.id.listid);
cxt=this;
try {
InputStream is=getResources().openRawResource(R.raw.events);
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
doc=db.parse(is,null);
NodeList main_nl=doc.getElementsByTagName("event");
for(int i=0;i<main_nl.getLength();i++){
Node st_node=main_nl.item(i);
NodeList sub_nl=st_node.getChildNodes();
for(int j=0;j<sub_nl.getLength();j++){
Node sub_node=sub_nl.item(j);
String name=sub_node.getNodeName();
if(name.equals("ename")){
value=sub_node.getFirstChild().getNodeValue();
names.add(value);
}
else if(name.equals("address")){
value=sub_node.getFirstChild().getNodeValue();
addresses.add(value);
}
}
}
}
catch (Exception e) {
System.out.println("XML Parsing Excpetion = " + e);
}
listadapter=new MyAdapter(getApplicationContext(),names,addresses);
list.setAdapter(listadapter);
} }
MyAdapter
public class MyAdapter extends BaseAdapter{
LayoutInflater inflater;
ArrayList<String> namess;
ArrayList<String> addressess;
public MyAdapter(Context c) {
// TODO Auto-generated constructor stub
con = c;
}
public MyAdapter(Context c, ArrayList<String> names,
ArrayList<String> addresses) {
// TODO Auto-generated constructor stub
namess= null;
this.addressess=null;
con=c;
this.addressess=addresses;
this.namess=names;
inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return namess.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
public TextView nam;
public TextView add;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v=convertView;
ViewHolder holder;
try
{
if (convertView == null)
{
v=inflater.inflate(R.layout.listitem,parent,false);
holder=new ViewHolder();
holder.nam=(TextView)v.findViewById(R.id.name);
holder.add = (TextView)v.findViewById(R.id.address);
v.setTag(holder);
}
else{
holder=(ViewHolder)v.getTag();
}
holder.nam.setText(namess.get(position).toString());
holder.add.setText(addressess.get(position).toString());
}
catch(OutOfMemoryError e)
{
System.gc();
}
return v;
}
private Context con;
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listid"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
создать floder raw.В этом создать events.xml
<?xml version="1.0" encoding="UTF-8"?>
<events>
<event>
<ename>Network Connect Grow</ename>
<address>Panera Bread, Orlando, Fl</address>
</event>
<event>
<ename>SRB Networking Meeting</ename>
<address>Mimi’s Café, Orlando, Fl</address>
</event>
</events>