像类别,子类别的Andr​​oid xml解析的ListView [英] xml parsing listview in android like category,subcategory

查看:126
本文介绍了像类别,子类别的Andr​​oid xml解析的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用XML解析开发Android的ListView。

i have to develop android listview using xml parsing.,

我得到XML饲料添加日常活动。

Am getting xml feed from mysql server.the product is added day to day activities.

在这里,我需要3种列表视图的:

Here i need 3 kind of listview:

1st view:   Category
2nd view:  SubCategory
3rd view:   Product

在fistview必须独自显示类别名称。

In fistview have to display Categoryname alone.

从我的XML数据源的 1视图有显示

From my xml feed 1st view have to display

类别名称单独

第二视图

有显示的 subcategoryname 属于类别名称。

Have to display subcategoryname belonging to Categoryname.

3ND查看

有显示产品名称和价格属于subcategoryname。

Have to display Product Name and Price belongs to subcategoryname.

这是我的第一个观点code:

This is my 1st view code:

public class MainActivity extends Activity {
static final String URL =http://api1.sfsfsfsf.com/dev/categories/?fields=&categoryid=3&access_token=bfb787a6e

        static String KEY_CATEGORY = "category";

       static final String KEY_TITLE = "categoryName";
        static final String KEY_SUBCATE = "categoryId";

 ListView lview3;
 LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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));
           map.put(KEY_SUBCATE, parser.getValue(e, KEY_SUBCATE));             
        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);
            Intent in = new Intent(
                    MainActivity.this,
                     com.example.androidcartxml.SubCate.class);
            in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
            in.putExtra(KEY_SUBCATE, map.get(KEY_SUBCATE));
               }  


     });

    }

         }

在这里,我已经显示在第一类列表view.also从1日以第二视图通过类别ID。

Here i have displayed category list in 1st view.also pass the categoryid from 1st view to 2nd view.

这是我的第二个观点code:

This is my 2nd view code:

       public class SubCate extends Activity {
         String mTitle;

         static final String URL = "http://api1.sfsfsff.com/dev/categories/?fields=&categoryid="+mTitle+"&access_token=bfb787a6e11";
            static String KEY_CATEGORY = "category";

              static final String KEY_TITLE = "categoryName";
             static final String KEY_NAME ="categoryId";
             static final String KEY_SUBCATE = "categoryId";
 ListView lview3;
 ListViewCustomAdapter adapter;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.subcate);
    Bundle b = getIntent().getExtras();
    //String s= getIntent().getStringExtra("orderid");
    mTitle = b.getString(KEY_SUBCATE);
    TextView grandtotal = (TextView) findViewById(R.id.subcate);
    grandtotal.setText("Welcome ," + mTitle );
    final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

   // String key = getIntent().getStringExtra(KEY_TITLE);

        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_NAME, parser.getValue(e, KEY_NAME));
            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);
            Intent in = new Intent(
                    SubCate.this,
                     com.example.androidcartxml.Catalogue.class);

            in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
            in.putExtra(KEY_NAME, map.get(KEY_NAME));
        //  in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL));
            //in.putExtra(KEY_PRICE, map.get(KEY_PRICE));
            startActivity(in);
        }  

     });

在第二个观点已经显示类别ID值从1日得到它activity.but我不得不提的是ID链接手段并没有得到子目录list.please给我一些这些解决方案。

In 2nd view have displayed categoryid value get it from 1st activity.but i have to mention that id to link means doesn't getting subcategory list.please give me some solution for these.

有关EG:

采取这是我的子类别链接:

take this is my subcategory link:

[http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=10&access_token=bfb787a6e1][1]

[http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=10&access_token=bfb787a6e1][1]

办法都显示子类别列表。

means have displayed subcategory list.

在这里,我必须通过从第一个到第二个该类别编号10(类别id = 10),并将其保存我的TextView。

Here i have to pass that category id 10(categoryid=10) from first to second and save it i textview.

    mTitle = b.getString(KEY_SUBCATE);
    TextView grandtotal = (TextView) findViewById(R.id.subcate);
       grandtotal.setText("Welcome ," + mTitle );

我有我的链接,这些mTitle值意味着它不会显示在subcategorylist.please帮助我。叫

I have to call these mTitle value on my link means it is not displayed on subcategorylist.please help me.

[http://api1.sfsfsff.com/dev/categories/?fields=&categoryid=\"+mTitle+\"&access_token=bfb787a6][2]

[http://api1.sfsfsff.com/dev/categories/?fields=&categoryid="+mTitle+"&access_token=bfb787a6][2]

什么是错在我的code.how可以拨打该类别ID到我的网址。请给我这些解决方案。

whats wrong in my code.how can call that categoryid to my url.please give me solution for these.

编辑:

这是我的网址:

[http://api1.sfsfsfsf.com/dev/categories/?fields=&categoryid=10&access_token=bfb787a6e][3]

[http://api1.sfsfsfsf.com/dev/categories/?fields=&categoryid=10&access_token=bfb787a6e][3]

我已经提到,直接类别id = 10的手段已经disaplyed所有的产品。

I have mentioned directly categoryid=10 means have disaplyed all product.

但我必须要改变这个网址,如:

But I have to change this url like:

[http://api1.sfsfsf.com/dev/categories/?fields=&categoryid=\"+mTitle+\"&access_token=bfb787a6e17][4]

[http://api1.sfsfsf.com/dev/categories/?fields=&categoryid="+mTitle+"&access_token=bfb787a6e17][4]

NW我已经改变了我的网址像类别ID =+ mTitle +表示不显示的所有产品。

Nw i have changed my url like categoryid="+mTitle+" means not displayed all products.

mTitle值从我的透水活动获得。

mTitle value is getting from my pervious activity.

什么是错在我的code。

whats wrong in my code.

我mTitle值是10.我不得不写了code等方法

my mTitle value is 10.i have to wrote the code like means

grandtotal.setText(欢迎+ mTitle);

它的成功显示mTitle值。

its displayed mTitle value successfully.

grandtotal.setText(欢迎+ URL); 指给我空值

推荐答案

在这里,我呼吁URL第一则只宣布mTitle value.thats为什么我在这里遇到的问题。

Here i have called URL first then only declared mTitle value.thats why i have faced problem here.

现在我得到了解决办法:

Now i got the solution:

我已经声明mTitle第一只afterthat呼吁网址,如下所示。

I have declared mTitle first afterthat only have called URL like below.

 mTitle = b.getString(KEY_SUBCATE);

 URL = "http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=" + mTitle +       "&access_token=bfb787a";

这篇关于像类别,子类别的Andr​​oid xml解析的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆