xml parsing listview in android like category,subcategory

大兔子大兔子 提交于 2019-12-26 15:06:25

问题


i have to develop android listview using xml parsing.,

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

Here i need 3 kind of listview:

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

In fistview have to display Categoryname alone.

From my xml feed 1st view have to display

Categoryname alone

In 2nd view:

Have to display subcategoryname belonging to Categoryname.

In 3nd View:

Have to display Product Name and Price belongs to subcategoryname.

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


     });

    }

         }

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

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

     });

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.

FOR EG:

take this is my subcategory link:

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

means have displayed subcategory list.

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

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]

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

EDIT:

This is my url:

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

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]

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

mTitle value is getting from my pervious activity.

whats wrong in my code.

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

grandtotal.setText("Welcome ," + mTitle );

its displayed mTitle value successfully.

grandtotal.setText("Welcome ," + URL ); means gave me null value


回答1:


i advice use sax parser instead of dom parser.

this link helpful to you. read this.




回答2:


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

Now i got the solution:

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


来源:https://stackoverflow.com/questions/14036254/xml-parsing-listview-in-android-like-category-subcategory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!