android ListView SimpleAdapter 带图片

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

 

item.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ItemImage"/>
<TextView
android:id="@+id/ItemTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20sp"/>
<TextView
android:id="@+id/ItemText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/ItemTitle"/>
</RelativeLayout>

 

主要代码

public class MainActivity extends Activity {

private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) this.findViewById(R.id.listView1);

ArrayList<HashMap<String, Object>> lst = new ArrayList<HashMap<String, Object>>();

for (int i = 0; i < 10; i++) {

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("Title", "标题" + i);

map.put("Content", "内容" + i);

map.put("Image", R.drawable.ic_launcher);

lst.add(map);

}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, lst,
R.layout.item, new String[] { "Title", "Content", "Image" },
new int[] { R.id.ItemTitle, R.id.ItemText, R.id.ItemImage });

listView.setAdapter(simpleAdapter);

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点了"+arg3, 1000).show();

}


});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

android ListView SimpleAdapter 带图片,布布扣,bubuko.com

android ListView SimpleAdapter 带图片

上一篇:[Android 新特性] Android 4.3新功能(正式发布前)


下一篇:关于Android 中 raw的用法以及与assets 的的区别和共同点