I have to pass the category,subcategory and product details through xml parsing in my android listview application.
This is my xml feed:
<Feed>
<category>
<Category>
<Categoryname>Books</Categoryname>
<SubCategory>
<subcategoryname>Internet</subcategoryname>
<Product>
<Name>New Masters of Flash</Name>
<Price>79.99</Price>
</Product>
<Product>
<Name>Professional Java Server Programming</Name>
<Price>63.99</Price>
</Product>
</SubCategory>
<SubCategory>
<subcategoryname>Software</subcategoryname>
<Product>
<Name>Krishnaveni</Name>
<Price>79.99</Price></Product>
<Product>
<Name>Veeman</Name>
<Price>63.99</Price>
</Product>
</SubCategory>
</Category>
</category>
</Feed>
Here i have faced some problems.
I have to run the app means
----> main activity have to displayed Books(Category) only.
----> i have to click the Books listview means am getting the (SubCategory).
They are:
Internet
Software
---->After i have to click Internet means am getting product of that particular(Internet) category.
They are:
New Masters of Flash
Professional Java Server Programming
If i have to click the 2nd subcategory(Software) means am i have to get the below results:
Krishnaveni
Veeman
Here only i have faced some problem.
Here i have to click internet subcategory means am getting 4 products.Same method is for software also.i have to click software subcategory means am getting 4 products.
How can i develop this.please help me.how can i get the product for that particular subcategory.
This is my coding part:
public class AndroidXMLParsingActivity extends Activity {
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml";
private static final int Tech = 0;
static String KEY_CATEGORY = "Category";
static final String KEY_TITLE = "Categoryname";
ListView lview3;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
songsList.add(map);
}
lview3 = (ListView) findViewById(R.id.listView1);
adapter = new LazyAdapter(this, songsList);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
switch (position)
{
case Tech:
Intent tech = new Intent(AndroidXMLParsingActivity.this, com.androidhive.xmlparsing.SubCate.class);
tech.putExtra(KEY_TITLE, "Books");
startActivity(tech);
break;
default:
break;
}
}
});
Second Activity:
public class SubCate extends Activity {
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml";
private static final int Tech = 0;
private static final int Sport =1;
static String KEY_CATEGORY = "SubCategory";
static final String KEY_SUBCATE = "subcategoryname";
ListView lview3;
ListViewCustomAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_SUBCATE, parser.getValue(e, KEY_SUBCATE));
songsList.add(map);
}
lview3 = (ListView) findViewById(R.id.listView1);
adapter = new ListViewCustomAdapter(this, songsList);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
switch (position)
{
case Tech:
Intent tech = new Intent(SubCate.this, com.androidhive.xmlparsing.Catalogue.class);
tech.putExtra(KEY_SUBCATE, "Internet");
startActivity(tech);
break;
case Sport:
Intent sport = new Intent(SubCate.this, com.androidhive.xmlparsing.Catalogue.class);
sport.putExtra(KEY_SUBCATE, "Software");
startActivity(sport);
break;
default:
break;
}
}
});
Third Activity:
public class Catalogue extends Activity {
// static String URL = "https://dl.dropbox.com/u/48258247/catalogue.json";
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml";
static String KEY_CATEGORY = "Product";
static final String KEY_TITLE = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_COST = "Price";
//static final String KEY_THUMB_URL = "Image";
ListView list;
ListAdapter adapter;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
// map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.listView1);
// Getting adapter by passing xml data ArrayList
adapter=new ListAdapter(this, songsList);
list.setAdapter(adapter);
System.out.println("Category is:- " + KEY_CATEGORY);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
}
});
please check the code and give me solution for this.please help me ya.
EDIT:
I have to add the code on my 2nd activity(SubCate.java)
first declare the KEY_TITLE
static final String KEY_TITLE = "Categoryname";
after onCreate function added below line:
Bundle bdl = getIntent().getExtras();
KEY_CATEGORY = bdl.getString(KEY_TITLE);
I have added some code on 3rd activity(Catalogue.java) also.
first declare the KEY_SUBCATE:
static final String KEY_SUBCATE = "subcategoryname";
after have to added below line after onCreate() function:
Bundle bdl = getIntent().getExtras();
KEY_CATEGORY = bdl.getString(KEY_SUBCATE);
Now i have to run the app means am doesn't get any products.please help me.how can i get the product on correct category.