【问题标题】:How to change array list to string array for using Universal Image Loader?如何将数组列表更改为字符串数组以使用通用图像加载器?
【发布时间】:2014-07-13 12:29:54
【问题描述】:

我是安卓新手。我只是尝试学习如何使用Universal Image Loader。 我发现Universal Image Loader中的图像url是静态设置的,或者是预设在String[] IMAGES中的。但我想从URL 更改这些图像。所以我完成了对XML 的解析并尝试从XML Web service 获取这些数据。

但是,我不知道如何将数组列表更改为字符串数组。有人可以教我吗?

xml web service代码如下:

class showCategoryTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            URL url = new URL("http://garyhui86.er-webs.com/monstersxml.php");
            HttpURLConnection urlConn = 
                                 (HttpURLConnection)url.openConnection();
            if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                DocumentBuilder builder = DocumentBuilderFactory
                        .newInstance().newDocumentBuilder();
                Document document = builder.parse(urlConn.getInputStream());
                NodeList nodeList = document.getElementsByTagName("Info");
                for (int i = 0; i < nodeList.getLength(); i++) {
                    NamedNodeMap attributes=nodeList.item(i).getAttributes();
                    String monstersname=attributes.getNamedItem("monsters_name").getNodeValue();
                    String monstersimage=attributes.getNamedItem("monsters_image").getNodeValue();
                    Log.i("ttt", monstersname);
                    Monsters_image.add(monstersimage);
                }
            }
            urlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

【问题讨论】:

  • 我不确定这是否有效。因为那不是应用程序中显示的图像。所以我想尝试了解 IMAGES 的数据。 TextView webContent = (TextView)root.findViewById(R.id.textView1); webContent.setText(图像);并且有一个错误。 TextView类型中的setText(CharSequence)方法不适用于参数(String[])

标签: java android arraylist arrays


【解决方案1】:

将您的字符串ArrayList 转换为字符串Array 喜欢

 String[] str=list.toArray(new String[list.size()]);

【讨论】:

  • 是不是「public static final String[] IMAGES = new String[] { urllink }; ”改为“String[] IMAGES =Monsters_image.toArray(new String[Monsters_image.size()]);”
  • @user3578548 是否要转换您的 Monsters_image 数组列表?那么就对了。
【解决方案2】:

试试这个方法

ArrayList<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("item2");
        String[] Arr = new String[list.size()];
        Arr = list.toArray(Arr);

【讨论】:

  • 是不是「public static final String[] IMAGES = new String[] { urllink }; 》改为「String[] IMAGES = new String[Monsters_image.size()]; Arr = Monsters_image.toArray(IMAGES);”
【解决方案3】:
String[] array = new String[list.size()];
int i = 0;
for (String string : list) {
    array[i++] = string;
}

【讨论】:

  • 当您用代码示例回答问题时,请考虑解释您的代码,以便有人可以从您的示例中学习,而不是简单地复制它。谢谢。
猜你喜欢
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2021-10-20
  • 2021-02-27
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
相关资源
最近更新 更多