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

Apps display listview(subcategory) depends upon selected list(category) in android sax parsing

krishnaveni

Well-Known Member
Dec 16, 2011
158
0
chennai
Hi.,

I have to develop an one application.
Here i have to display the listview(Category) on my first page.If i have to select any category which means have to display the subcategory depends upon selected category on 2nd page.

i have follows following xml feed:

http://dev.mercuryminds.com/xctesting/feed.xml


Here i have handling my xml parser using below code:

[HIGH]public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

buffer = new StringBuffer();

if (localName.equalsIgnoreCase("Category")) {
category = new Categories();
category.setCategoryTitle(attributes.getValue("title"));
System.out.println("Category name is"+" " +attributes.getValue("title"));
category.setCategoryImage(attributes.getValue("image"));
System.out.println("Category image is"+" "+attributes.getValue("image"));
}

if (localName.equalsIgnoreCase("Subcategory")) {
subcategory = new Categories();
subcategory.setSubCategorytitle(attributes.getValue("name"));
System.out.println("SubCategory name is"+" " +attributes.getValue("name"));
subcategory.setCategoryImage(attributes.getValue("subimage"));
System.out.println("SubCategory image is"+" " +attributes.getValue("subimage"));
}
[/HIGH]
This is my main activity:

[HIGH] public class HomePage extends Activity implements
OnItemClickListener {

GridView categorylist;
List<Categories> categories;
CategoriesAdapter listViewAdapter;

static final String URL = "http://dev.mercuryminds.com/xctesting/feed.xml";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
categorylist.setOnItemClickListener(this);
}

private void findViewsById() {
categorylist = (GridView) findViewById(R.id.categorylist);
GetXMLTask task = new GetXMLTask(this);
task.execute(new String[] { URL });
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

}




private class GetXMLTask extends AsyncTask<String, Void, List<Categories>> {
private Activity context;
public GetXMLTask(Activity context) {
this.context = context;
}

@Override
protected void onPostExecute(List<Categories> categories) {
listViewAdapter = new CategoriesAdapter(context, categories);
categorylist.setAdapter(listViewAdapter);
}


private String getXmlFromUrl(String urlString) {
StringBuffer output = new StringBuffer("");
try {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(stream));
String s = "";
while ((s = buffer.readLine()) != null)
output.append(s);
}

} catch (Exception ex) {
ex.printStackTrace();
}
return output.toString();

}

@Override
protected List<Categories> doInBackground(String... urls) {
List<Categories> categories = null;
String xml = null;
for (String url : urls) {
xml = getXmlFromUrl(url);
InputStream stream = new ByteArrayInputStream(xml.getBytes());
categories = SAXXMLParser.parse(stream);

for (Categories category : categories) {
String imageURL = category.getCategoryImage();
Bitmap bitmap = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;

try {
bitmap = BitmapFactory
.decodeStream(new
URL(imageURL).openStream(),
null, bmOptions);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
category.setCategoryImageBitmap(bitmap);

}
}
return categories;
}
}

[/HIGH]
I wish to need the o/p like below:

In first page have to display categories:

door curtains
children curtains

if i have to click children curtains which means have to display subcategories on 2nd page:

padvk
kannuku


But now am getting the o/p like:

http://dev.mercuryminds.com/xctesting/grid.png

please check the o/p ..why am getting the o/p like above.How can i resolve these error ??? How can i get the correct output ???


This is my main.xml file:

<GridView
android:id="@+id/categorylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp" >
</GridView>

This is my list_item.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:eek:rientation="vertical" >

<RelativeLayout
android:id="@+id/categories"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:eek:rientation="vertical" >

<ImageView
android:id="@+id/categoryimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>

<TextView
android:id="@+id/categorytitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/categoryimage"
android:background="@android:color/darker_gray"
android:text="Category"
android:textColor="#CC0033"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
</RelativeLayout>

[1]: http://dev.mercuryminds.com/xctesting/feed.xml
[2]: http://i.stack.imgur.com/H0OWA.png
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones