Assets Folder:- abc.xml File
<main>
<student>
<name> Amit sharma</name>
<address>mathura</address>
<qua> MCA</qua>
</student>
<student>
<name> Amit kumar sharma </name>
<address> Raya :- Nagla Bhima </address>
<qua> BCA </qua>
</student>
<student>
<name> Amit</name>
<address>16 nagla bhima </address>
<qua> B Tech</qua>
</student>
<student>
<name> ABC You are</name>
<address>This is my address</address>
<qua> B.Com </qua>
</student>
<student>
<name> HIMCS</name>
<address> MCA MATHURA</address>
<qua> BCA </qua>
</student>
<student>
<name> Mathura Krishana</name>
<address> This is me </address>
<qua> M Tech </qua>
</student>
</main>
Layout mainActivity:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listview"
android:cacheColorHint="@android:color/transparent"
android:dividerHeight="2dp"
/>
</RelativeLayout>
listitemacticity:-
<?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" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
<TextView
android:id="@+id/quali"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
</LinearLayout>
MainActivity:-
package offlinereaddata.amit.com;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
bindDataToListing();
}
private void bindDataToListing() {
// TODO Auto-generated method stub
try {
SAXParserFactory saxparser = SAXParserFactory.newInstance();
SAXParser parser = saxparser.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
ParsingClass pc = new ParsingClass();
xmlReader.setContentHandler(pc);
InputStream is = getAssets().open("abc.xml");
xmlReader.parse(new InputSource(is));
BindingData bindingData = new BindingData(this, pc.name,
pc.address, pc.qua);
listView.setAdapter(bindingData);
} catch (Exception e) {
e.getMessage();
}
}
}
class BindingData:-
package offlinereaddata.amit.com;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BindingData extends BaseAdapter{
ArrayList<String> name;
ArrayList<String> address;
ArrayList<String> qua;
LayoutInflater inflater;
public BindingData() {
}
public BindingData(Activity act, ArrayList<String> name,
ArrayList<String> add, ArrayList<String> qua) {
this.name = name;
this.address = add;
this.qua = qua;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.address);
holder.txtQua = (TextView) convertView.findViewById(R.id.quali);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.txtName.setText(Html.fromHtml("" + name.get(position)));
holder.txtAddress.setText(Html.fromHtml("<b>Address : </b>"
+ address.get(position)));
holder.txtQua.setText(Html.fromHtml("<b>Qualification : </b>"
+ qua.get(position)));
return convertView;
}
private class Holder {
TextView txtName, txtAddress, txtQua;
}
}
ParsingClass:-
package offlinereaddata.amit.com;
import java.util.ArrayList;
import java.util.jar.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParsingClass extends DefaultHandler {
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> address = new ArrayList<String>();
ArrayList<String> qua = new ArrayList<String>();
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, (org.xml.sax.Attributes) attributes);
if (localName.equalsIgnoreCase("name")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("address")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("qua")) {
tempStore = "";
}else{
tempStore = "";
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equalsIgnoreCase("name")) {
name.add(tempStore);
} else if (localName.equalsIgnoreCase("address")) {
address.add(tempStore);
} else if (localName.equalsIgnoreCase("qua")) {
qua.add(tempStore);
}
tempStore = "";
}
private String tempStore = "";
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
tempStore += new String(ch, start, length);
}
}
Thanks Friends
This is very use full for you this is easy to understand ........
<main>
<student>
<name> Amit sharma</name>
<address>mathura</address>
<qua> MCA</qua>
</student>
<student>
<name> Amit kumar sharma </name>
<address> Raya :- Nagla Bhima </address>
<qua> BCA </qua>
</student>
<student>
<name> Amit</name>
<address>16 nagla bhima </address>
<qua> B Tech</qua>
</student>
<student>
<name> ABC You are</name>
<address>This is my address</address>
<qua> B.Com </qua>
</student>
<student>
<name> HIMCS</name>
<address> MCA MATHURA</address>
<qua> BCA </qua>
</student>
<student>
<name> Mathura Krishana</name>
<address> This is me </address>
<qua> M Tech </qua>
</student>
</main>
Layout mainActivity:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listview"
android:cacheColorHint="@android:color/transparent"
android:dividerHeight="2dp"
/>
</RelativeLayout>
listitemacticity:-
<?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" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
<TextView
android:id="@+id/quali"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sameer Ahmad"
android:textSize="18dp" />
</LinearLayout>
MainActivity:-
package offlinereaddata.amit.com;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
bindDataToListing();
}
private void bindDataToListing() {
// TODO Auto-generated method stub
try {
SAXParserFactory saxparser = SAXParserFactory.newInstance();
SAXParser parser = saxparser.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
ParsingClass pc = new ParsingClass();
xmlReader.setContentHandler(pc);
InputStream is = getAssets().open("abc.xml");
xmlReader.parse(new InputSource(is));
BindingData bindingData = new BindingData(this, pc.name,
pc.address, pc.qua);
listView.setAdapter(bindingData);
} catch (Exception e) {
e.getMessage();
}
}
}
class BindingData:-
package offlinereaddata.amit.com;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BindingData extends BaseAdapter{
ArrayList<String> name;
ArrayList<String> address;
ArrayList<String> qua;
LayoutInflater inflater;
public BindingData() {
}
public BindingData(Activity act, ArrayList<String> name,
ArrayList<String> add, ArrayList<String> qua) {
this.name = name;
this.address = add;
this.qua = qua;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.address);
holder.txtQua = (TextView) convertView.findViewById(R.id.quali);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.txtName.setText(Html.fromHtml("" + name.get(position)));
holder.txtAddress.setText(Html.fromHtml("<b>Address : </b>"
+ address.get(position)));
holder.txtQua.setText(Html.fromHtml("<b>Qualification : </b>"
+ qua.get(position)));
return convertView;
}
private class Holder {
TextView txtName, txtAddress, txtQua;
}
}
ParsingClass:-
package offlinereaddata.amit.com;
import java.util.ArrayList;
import java.util.jar.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParsingClass extends DefaultHandler {
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> address = new ArrayList<String>();
ArrayList<String> qua = new ArrayList<String>();
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, (org.xml.sax.Attributes) attributes);
if (localName.equalsIgnoreCase("name")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("address")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("qua")) {
tempStore = "";
}else{
tempStore = "";
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equalsIgnoreCase("name")) {
name.add(tempStore);
} else if (localName.equalsIgnoreCase("address")) {
address.add(tempStore);
} else if (localName.equalsIgnoreCase("qua")) {
qua.add(tempStore);
}
tempStore = "";
}
private String tempStore = "";
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
tempStore += new String(ch, start, length);
}
}
Thanks Friends
This is very use full for you this is easy to understand ........
No comments:
Post a Comment