0

我必须在我的 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>

在这里我遇到了一些问题。

我必须运行应用程序意味着

----> 主要活动必须仅显示书籍(类别)。----> 我必须单击 Books listview 意味着正在获取(子类别)。他们是:

Internet
Software

---->在我必须单击Internet后,意味着我正在获取该特定(Internet)类别的产品。他们是:

New Masters of Flash
Professional Java Server Programming

如果我必须单击第二个子类别(软件)意味着我必须得到以下结果:

Krishnaveni
Veeman

在这里只有我遇到了一些问题。

在这里,我必须单击 Internet 子类别意味着获得 4 个产品。同样的方法也适用于软件。我必须单击软件子类别意味着获得 4 个产品。

我如何开发这个。请帮助我。我怎样才能获得该特定子类别的产品。

这是我的编码部分:

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 &lt;song&gt;
        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;
         }
        }  

     });

第二个活动:

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 &lt;song&gt;
        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 =&gt; 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;
         }
        }  

     });

第三项活动:

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 &lt;song&gt;
        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 =&gt; 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);

            }

        });

请检查代码并为我提供解决方案。请帮助我。

编辑:

我必须在我的第二个活动中添加代码(SubCate.java)

首先声明 KEY_TITLE

 static final String KEY_TITLE = "Categoryname";

在 onCreate 函数添加以下行之后:

    Bundle bdl = getIntent().getExtras();
    KEY_CATEGORY = bdl.getString(KEY_TITLE);

我还在第三个活动(Catalogue.java)上添加了一些代码。

首先声明 KEY_SUBCATE:

static  final String KEY_SUBCATE = "subcategoryname";

在 onCreate() 函数之后必须添加以下行:

      Bundle bdl = getIntent().getExtras();
      KEY_CATEGORY = bdl.getString(KEY_SUBCATE);

现在我必须运行该应用程序意味着我没有得到任何产品。请帮助我。我怎样才能获得正确类别的产品。

4

1 回答 1

0

只需final在点击子类别之前制作每个产品。我认为您的问题将得到解决。如果您不使这些最终值每次都排在最后位置。

我想你在这里发送产品表格:---

      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));

只需更换它:---

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);
于 2012-12-04T05:21:11.950 回答