简体   繁体   中英

Getting same values in list while parsing xml using xmlpullparser

I am trying to parse an xml file present in asset folder.All seems good and there is no error but the problem is that it gives same values to the list for every node in xml.As i feel it overwrite previous value.I am stuck here.Even after a lot of googling could not find any solution. Any help would be appreciated.

Below is the code:-

Product.java

package com.example.webservicedemo;

public class product {


    private static String productname="";
    private static String productcolor="";
    private static String productquantity="";



    public static String getProductname() {
        return productname;
    }
    public static void setProductname(String productname) {
        product.productname = productname;
    }
    public static String getProductcolor() {
        return productcolor;
    }
    public static void setProductcolor(String productcolor) {
        product.productcolor = productcolor;
    }
    public static String getProductquantity() {
        return productquantity;
    }
    public static void setProductquantity(String productquantity) {
        product.productquantity = productquantity;
    }
}

Below is the code of parser:- productXmlPullParser.java

package com.example.webservicedemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.content.res.AssetManager;
import android.widget.Toast;
import com.example.webservicedemo.*;
public class productXmlPullParser {

    static final String KEY_PRODUCT = "product";
    static final String KEY_NAME = "productname";
    static final String KEY_COLOR = "productcolor";
    static final String KEY_QUANTITY = "productquantity";


    public static List<product> getproductsFromFile(Context ctx) {
        String tagname;
        List<product> Products;
        Products = new ArrayList<product>();

        product curproduct = null;
        String curText = "";
        InputStream is = null;
        try {
            is = ctx.getAssets().open("temp.xml");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();

        }


        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            xpp.setInput(reader);

            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                 tagname = xpp.getName();

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase(KEY_PRODUCT)) {

                        curproduct = new product();
                    }
                    break;

                case XmlPullParser.TEXT:
                    //grab the current text so we can use it in END_TAG event
                    curText = xpp.getText();


                    break;

                case XmlPullParser.END_TAG:

                    if (tagname.equalsIgnoreCase(KEY_PRODUCT)) {
                        // if </site> then we are done with current Site
                        // add it to the list.
                        Products.add(curproduct);
//                      Toast.makeText(ctx, curproduct+"",3000).show();

                    } else if (tagname.equalsIgnoreCase(KEY_NAME)) {
                        // if </name> use setName() on curSite
                        product.setProductname(curText);
                        Toast.makeText(ctx, curText+"",3000).show();

                    } else if (tagname.equalsIgnoreCase(KEY_COLOR)) {
                        // if </link> use setLink() on curSite
                        curproduct.setProductcolor(curText);
                        Toast.makeText(ctx, curText+"",3000).show();
                    } else if (tagname.equalsIgnoreCase(KEY_QUANTITY)) {
                        // if </about> use setAbout() on curSite
                        curproduct.setProductquantity(curText);
                        Toast.makeText(ctx, curText+"",3000).show();

                    } 

                    break;

                default:
                    break;
                }
                //move on to next iteration
                eventType = xpp.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(ctx, "error", 6000).show();
        }

        // return the populated list.
        return Products;
    }
}

Below is the code of my custom arrayadapter:--

ProductAdapter

    package com.example.webservicedemo;

    import java.util.List;

    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.RelativeLayout;
    import android.widget.TextView;



    public class UsersAdapter extends ArrayAdapter<product> {

        public UsersAdapter(Context context, int resource, List<product> objects) {
            super(context, resource, objects);
            // TODO Auto-generated constructor stub
        }


        @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            RelativeLayout row = (RelativeLayout)convertView;
            Log.i("Users", "getView pos = " + pos);
            if(null == row){
                LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = (RelativeLayout)inflater.inflate(R.layout.listitem, null);
            }

            TextView Email = (TextView)row.findViewById(R.id.txtEmail);
            TextView Password = (TextView)row.findViewById(R.id.txtPassword);
            TextView Deviceid = (TextView)row.findViewById(R.id.txtDeviceid);
            Email.setText(getItem(pos).getProductname());
            Password.setText(getItem(pos).getProductcolor());
            Deviceid.setText(getItem(pos).getProductquantity());

            return row;

        }

    }

Below is the main activity where i am trying to get the result values in a list

MainActivity.java

package com.example.webservicedemo;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.example.webservicedemo.productXmlPullParser;;;

public class UserList extends Activity {
UsersAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xmlparsingdemo);


        ListView lvUser=(ListView)findViewById(R.id.lvUsers);
        mAdapter=new UsersAdapter(getApplicationContext(),R.layout.listitem,productXmlPullParser.getproductsFromFile(getApplicationContext()));
        lvUser.setAdapter(mAdapter);
        Toast.makeText(getApplicationContext(), mAdapter.getCount()+"",3000).show();

    }
}

Here is the xml which i am trying to parse:--

temp.xml

<?xml version="1.0" encoding="UTF-8"?>
<products>
    <product>     
        <productname>Jeans</productname>
        <productcolor>red</productcolor>
        <productquantity>5</productquantity>
    </product>
    <product>     
        <productname>Tshirt</productname>
        <productcolor>blue</productcolor>
        <productquantity>3</productquantity>
    </product>
    <product>     
        <productname>shorts</productname>
        <productcolor>green</productcolor>
        <productquantity>4</productquantity>
    </product>

</products>

But the output is like something like below screenshot:-

在此处输入图片说明

After a lot of effort i could not find what went wrong.

Any help would be appreciated.

Problem in yours class "product". Fields are static, each field contains one last value for all instances. Remove "static" modificator, and all will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM