1

Sorry, if I've made things confusing. I edited the whole question.

I'm using XMLPARSER for my urls that is hosted on my webserver.

I would like to parse my url list from url.add(map); into intent.putExtra(Extra.IMAGES, imageUrls); So that i could retrieve urls from my XMLparser. Please guide me how to do it with codes. My attempt shows error

java.lang.ArrayStoreException: source[0] of type java.util.HashMap cannot be stored in destination array of type java.lang.String[]

public class HomeActivity extends BaseActivity {

        static final String URL = "http://*******";


        static final String KEY_LIST = "list";
        static final String KEY_THUMB_URL = "thumb_url";



        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ac_home);

            ArrayList<HashMap<String, String>> url = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL);
            Document doc = parser.getDomElement(xml); 
            NodeList nl = doc.getElementsByTagName(KEY_LIST);

            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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                url.add(map); // <***** Set the results from here to imageUrls
            }

        imageUrls = (String[]) url.toArray(new String[0]); // <----***Error****

        }
        public void onImageGridClick(View view) {
            Intent intent = new Intent(this, ImageGridActivity.class);
            intent.putExtra(Extra.IMAGES, imageUrls); 
            startActivity(intent);
        }

        public void onImagePagerClick(View view) {
            Intent intent = new Intent(this, ImagePagerActivity.class);
            intent.putExtra(Extra.IMAGES, imageUrls);
            startActivity(intent);
        }

    }
5
  • Anyone free to take a look on my codes? Commented Aug 13, 2012 at 6:39
  • Be specific in your question "What you want ?", your question is confusing. If you want to add only XML-parser URLs then don't add URLs from heavy_images.xml, and one more thing understand things not directly copy it from GitHub or any code source. Commented Aug 13, 2012 at 7:23
  • sorry, I've edited my question. Please guide me Android Coder Commented Aug 13, 2012 at 7:36
  • I've tried to put in this imageUrls = (String[]) url.toArray(new String[0]); but it shows error "java.lang.ArrayStoreException: source[0] of type java.util.HashMap cannot be stored in destination array of type java.lang.String[]" Commented Aug 13, 2012 at 8:08
  • for this you have to set type of arraylist to String instead of hashmap Commented Aug 13, 2012 at 8:10

1 Answer 1

1

Try this code

public class MainActivity extends BaseActivity {

    String[] imageUrls;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_home);
        ArrayList<String> url = new ArrayList<String>();
        try {
            URL url_link = new URL("http:******");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url_link.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("list");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                Element fstElmnt = (Element)node;
                NodeList nameList = fstElmnt.getElementsByTagName("thumb_url");
                Element nameElement = (Element)nameList.item(0);
                nameList = nameElement.getChildNodes();

                url.add(nameList.item(0).getNodeValue());
            }
            imageUrls = (String[]) url.toArray(new String[0]);
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

    }
    public void onImageGridClick(View view) {
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(Extra.IMAGES, imageUrls); 
        startActivity(intent);
    }

    public void onImagePagerClick(View view) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(Extra.IMAGES, imageUrls);
        startActivity(intent);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot Android Coder . You deserve 5 stars from me. THANKS a million times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.