Android中ListView中显示图片和文本

 

ListView中一行的格式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent">
 <ImageView android:layout_width="wrap_content" android:id="@+id/imageView"
  android:layout_height="wrap_content"></ImageView>
 <TextView android:id="@+id/textView" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_gravity="center"></TextView>
</LinearLayout>

 

public class MainActivity extends ListActivity {
 
 private final String cities[] = new String[]{"beijing","shanghai","jiangsu","guangdong","zhejiang","chongqiong","sichun","hunan"};
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
      
        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
        for(int i=0; i<cities.length ; i++){
          Map<String,Object> map = new HashMap<String,Object>();
          map.put("icon", R.drawable.icon);
          map.put("city", cities[i]);
          data.add(map);
        }
       
       
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.row, new String[]{"icon","city"}, new int[]{R.id.imageView,R.id.textView});
        setListAdapter(adapter);
    }
}

 

你可能感兴趣的:(xml,android)