无法将图片从网址添加到列表视图中

时间:2015-02-10 19:35:00

标签: android xml image parsing listview

我正在构建一个应用程序,它将一些图像和信息显示在列表视图中。 这是我的主要活动,

// All static variables
static final String URL = "http://starkgot.host56.com/first.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_DATE = "date";
static final String KEY_DESCRIPTION = "description";
static final String KEY_DIRECTOR = "director";
static final String KEY_CAST = "cast";
static final String KEY_video = "video";
static final String KEY_image = "image";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
ImageView imageview12=(ImageView)findviewbyid(R.id.item_icon);

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

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    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_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
        map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
        map.put(KEY_DIRECTOR, parser.getValue(e, KEY_DIRECTOR));
        map.put(KEY_CAST, parser.getValue(e, KEY_CAST));
        map.put(KEY_video, parser.getValue(e, KEY_video));
        map.put(KEY_image, parser.getValue(e, KEY_image));

        // adding HashList to ArrayList
        menuItems.add(map);
    }
Picasso.with(this).load(KEY_image).into(imageview12)   

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item, new String[] { KEY_NAME, KEY_DATE,
                    KEY_DESCRIPTION, KEY_DIRECTOR, KEY_CAST, KEY_video,KEY_image}, new int[] { R.id.name, R.id.date,
                    R.id.description, R.id.director, R.id.cast, R.id.video,R.id.image});//image not getting loaded here!!!

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String date = ((TextView) view.findViewById(R.id.date))
                    .getText().toString();
            String description = ((TextView) view
                    .findViewById(R.id.description)).getText().toString();
            String director = ((TextView) view.findViewById(R.id.director))
                    .getText().toString();
            String cast = ((TextView) view.findViewById(R.id.cast))
                    .getText().toString();
            String video = ((TextView) view.findViewById(R.id.video))
                    .getText().toString();
            String image = ((TextView) view.findViewById(R.id.image))
                    .getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_DATE, date);
            in.putExtra(KEY_DESCRIPTION, description);
            in.putExtra(KEY_DIRECTOR, director);
            in.putExtra(KEY_CAST, cast);
            in.putExtra(KEY_video, video);
            in.putExtra(KEY_image, image);

            startActivity(in);

        }

    });

}
}

正确完成XML解析,并且单击列表上每个项目后显示的活动的第二个类也正确完成。 如何添加图像因子,就像我的列表视图中的名称描述等其他因素一样。

1 个答案:

答案 0 :(得分:0)

只需使用任何图片加载库。

我建议使用&#34;通用图片加载器&#34;,检查this

如果您在实施方面遇到问题,请使用this了解如何使用它。