Android ListView+image的使用

首先创建layout部局文件xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    
         android:id="@+id/RelativeLayout01"    
         android:layout_width="fill_parent"    
         xmlns:android="http://schemas.android.com/apk/res/android"    
         android:layout_height="wrap_content"    
         android:paddingBottom="4dip"    
         android:paddingLeft="12dip"> 
<ImageView    
     android:layout_width="wrap_content"    
     android:layout_height="wrap_content"    
     android:id="@+id/ItemImage">    
</ImageView> 

<TextView
     android:layout_height="wrap_content"    
     android:layout_width="fill_parent"    
     android:id="@+id/ItemTitle"   
      android:layout_toRightOf="@+id/ItemImage"
     android:textSize="30dip"
></TextView> 
<TextView    
     android:layout_height="wrap_content"    
     android:layout_width="fill_parent" 
      android:layout_toRightOf="@+id/ItemImage" 
      android:layout_below="@+id/ItemTitle"
     android:id="@+id/ItemText"
></TextView> 

</RelativeLayout>

接下来在main.xml文件中布局listView即可这里就不用给源代码了;

在下面一步中导入你想要的png图片存放在drawable中

最后就是我们的.java文件的编写了,我们知道listView需要用到ArrayAdapter——》这个是创建listVIew的一种方法,在这里我们运用ArrayList与SimpleAdapter相结合来创建具有图片的listView

代码如下:

ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); //这里的String表示对第二个字段的标题,后面的Object可以扩展为其他类对象

        for(int i=0;i<15;i++)   
        {   
            HashMap<String, Object> map = new HashMap<String, Object>();   
            map.put("icon", 这里是图片的id(R.drawable.x1));   
            map.put("ItemTitle", "标题字段");   
            map.put("ItemText", "显示文字字段");   
            mylist.add(map);   
        } 
m_listView.setAdapter(initAdapter()); //这里我们调用initAdapter()函数源码如下

public SimpleAdapter initAdapter(){

        //生成适配器,数组===》ListItem   
        SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释   
                                                    mylist,//数据来源    
                                                    R.layout.my_layout,//ListItem的XML实现   
                                                    new String[] {"icon","ItemTitle", "ItemText"},    
                                                    new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});   
        //添加并且显示 
        return mSchedule;
}

这里我们还可以通过添加删除

public boolean onCreateOptionsMenu(Menu menu) {
   // TODO Auto-generated method stub
   menu     
   .add(0, 0, 0, "添加")         
   .setIcon(android.R.drawable.ic_media_next); 
   menu     
   .add(0, 1, 1, "删除")         
   .setIcon(android.R.drawable.ic_media_next);

   return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
   case 0:
            HashMap<String, Object> map = new HashMap<String, Object>();   
            map.put("icon", R.drawable.x1);   
            map.put("ItemTitle", "城市");   
            map.put("ItemText", "天气现象+温度");   
            mylist.add(map);   
    m_listView.setAdapter(initAdapter()); 
    break;
   case 1:
    mylist.remove(m_listView.getSelectedItem());
    m_listView.setAdapter(initAdapter()); 
    break;
   default:
    break;
   }
   return super.onOptionsItemSelected(item);
}
原文地址:https://www.cnblogs.com/vip-ygh/p/3569627.html