2015-02-10 109 views
0

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

// 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解析处理得当,并在列表中点击也得当每个项目后显示活动的第二类。 如何在我的列表视图中添加图像因子,就像名称描述等其他因素一样。

+0

据我所知,你想通过一个图像,因为你传递名称到下一个活动,我说得对吗?如果我不知道,那么你需要将URL路径传递给图像,就像你将其他所有附加信息传递给该图像一样,然后在另一个活动中,当接收到图像URL时,将其重新分配到列表中,完成后通常使用适配器来处理列表视图的所有内容。请从Vogella“http://www.vogella.com/tutorials/AndroidListView/article.html”中查看本教程,该教程解释了很多关于列表视图以及如何使用它们的信息。 – 2015-02-10 19:46:17

+0

不,这不是我想要的,我想在此活动本身的列表视图上显示图像。 – 2015-02-11 08:22:38

+0

那么,在你的活动中,存储你想要显示的信息,可能是一个带有URL的ArrayList,或者类似的东西,然后用它作为适配器来填充你的列表。 – 2015-02-11 14:25:44

回答

0

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

我建议使用“Universal Image Loader”,检查this

如果您在实施时发现问题,请使用this来了解如何使用它。