[英]display product is belongs to category in android listview using xml parsing
I have to pass the category,subcategory and product details through xml parsing in my android listview application. 我必须通过android listview应用程序中的xml解析传递类别,子类别和产品详细信息。
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).
---->我必须单击“书籍”列表视图,这意味着正在获取(SubCategory)。 They are:
他们是:
Internet
Software
---->After i have to click Internet means am getting product of that particular(Internet) category. ---->在我必须单击Internet之后,就意味着正在获取该特定(Internet)类别的产品。 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. 在这里,我必须单击Internet子类别意味着要获得4个产品。同样的方法也适用于软件。我必须单击软件子类别意味着要获得4个产品。
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) 我必须在第二个活动中添加代码(SubCate.java)
first declare the KEY_TITLE 首先声明KEY_TITLE
static final String KEY_TITLE = "Categoryname";
after onCreate function added below line: 在行下面添加onCreate函数之后:
Bundle bdl = getIntent().getExtras();
KEY_CATEGORY = bdl.getString(KEY_TITLE);
I have added some code on 3rd activity(Catalogue.java) also. 我还在第三活动(Catalogue.java)上添加了一些代码。
first declare the KEY_SUBCATE: 首先声明KEY_SUBCATE:
static final String KEY_SUBCATE = "subcategoryname";
after have to added below line after onCreate() function: 之后必须在onCreate()函数之后添加以下行:
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. 现在我必须运行该应用程序意味着没有任何产品。请帮助我。我如何才能将产品归类到正确的类别。
Just Make the final
every product before clicking on sub Category . 在单击子类别之前,只需制作
final
每个产品即可。 I think your problem will be solved.If you will not make these final values will be take last position every time. 我认为您的问题会得到解决。如果您不做这些最终值,则每次都会取最后一个位置。
I think you send product form here:--- 我想您在这里发送产品表格:---
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));
Just Replace of it:--- 只需替换它:---
final String key_title=parser.getValue(e, KEY_TITLE);
final String key_desc=parser.getValue(e, KEY_DESCRIPTION);
fiinal String key_cost=parser.getValue(e, KEY_COST);
map.put(KEY_TITLE, key_title);
map.put(KEY_DESCRIPTION, key_desc);
map.put(KEY_COST, key_cost);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.