• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android xml parsing using sax

krishnaveni

Well-Known Member
I have to develop one android application.

Here i have to create gridview...In gridview layout have one textview and one gallery view...In these gallery view have one imageview and one textview...

These is my main_activity.xml file:

[HIGH]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical">

<GridView
android:id="@+id/listView1"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
/>
</RelativeLayout>
[/HIGH]

list_item.xml:


[HIGH]<?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"
>

<Gallery
android:id="@+id/model"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/brand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#CC0033"
android:textSize="16sp"

android:textStyle="bold" />
</LinearLayout>
[/HIGH]

gallery_item.xml:

[HIGH] <?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"
>

<ImageView
android:id="@+id/model1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />


<TextView
android:id="@+id/articletitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#CC0033"
android:textSize="16sp"

android:textStyle="bold" />
</LinearLayout>

[/HIGH]

This is my adpater java file:

[HIGH] public class CustomListViewAdapter extends ArrayAdapter<Laptop> {
Activity context;
List<Laptop> laptops;

public CustomListViewAdapter(Context context2, List<Laptop> laptops) {
super(context2, R.layout.list_item, laptops);
this.context = (Activity) context2;
this.laptops = laptops;
}

/*private view holder class*/
private class ViewHolder {
ImageView imageView;
Gallery txtModel;

TextView txtBrand;
TextView txtPrice;
}

public Laptop getItem(int position) {
return laptops.get(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtModel = (Gallery) convertView.findViewById(R.id.model);
holder.txtBrand = (TextView) convertView.findViewById(R.id.brand);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
listViewAdapter = new ListAdapter(context, laptops);

Laptop laptop = (Laptop) getItem(position);
holder.txtModel.setAdapter(listViewAdapter);
holder.txtBrand.setText(laptop.getBrand());

return convertView;
}
}
[/HIGH]

This is my ListAdapter.java class:
[HIGH]public class ListAdapter extends ArrayAdapter<Laptop> {
Activity context;
List<Laptop> laptops;

public ListAdapter(Activity context, List<Laptop> laptops) {
super(context, R.layout.listrow, laptops);
this.context = context;
this.laptops = laptops;
}

/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtModel;

TextView txtBrand;
TextView txtPrice;
}

public Laptop getItem(int position) {
return laptops.get(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.txtModel = (TextView) convertView.findViewById(R.id.articletitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Laptop laptop = (Laptop) getItem(position);
holder.txtModel.setText(laptop.getModel());
return convertView;
}
[/HIGH]

SAXHandler.java:
[HIGH]
public class SAXXMLHandler extends DefaultHandler {
private List<Laptop> laptops;

// private List<Laptop> articles;
public boolean current = false;
public String currentValue = null;
boolean isArticle,isThumbURL;
// to maintain context
private Laptop laptop;
private Laptop article;
private Laptop image;
public SAXXMLHandler() {
laptops = new ArrayList<Laptop>();
}

public List<Laptop> getLaptops() {
return laptops;
}


// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
current = true;
if (localName.equals("Categories")) {
// create a new instance of Laptop
laptop = new Laptop();
laptops = new ArrayList<Laptop>();
}

else if (localName.equals("Category")) {
// create a new instance of Laptop
laptop = new Laptop();
laptop.setBrand(attributes.getValue("name"));

}
else if (localName.equals("Articles")) {
// create a new instance of Laptop
article = new Laptop();
isArticle = true;

}

else if (localName.equals("article")) {

// create a new instance of Laptop
article = new Laptop();
article.setModel(attributes.getValue("title"));

}

}

public void characters(char[] ch, int start, int length)
throws SAXException {
if(current)
{
currentValue = new String(ch, start, length);
current=false;
}
}

public void endElement(String uri, String localName, String qName)
throws SAXException {
current = false;
if (localName.equals("Categories")) {
// add it to the list
// laptops.add(laptop);

}

else if (localName.equals("Category")) {
// add it to the list
laptops.add(laptop);


}
else if (localName.equals("Articles")) {
// add it to the list
isArticle = false;


}
else if (localName.equals("article")) {
laptops.add(article);


}

}
}
[/HIGH]

I have to run the app means am getting the category name well...also all article title is displayed on each and every category on many times..

Why this is displayed like many times..but i wish to display the article title on corresponding category ??? how can i do ???



please give me the solution for these ???
 
Back
Top Bottom