Android -- listview的使用, Inflater打气筒创建View对象,三种数据Adapter

时间:2022-06-28 04:01:16

1. 代码示例,( 数据库来源上篇的数据)

MainActivity.java

public class MainActivity extends Activity {

	private ListView lv1 = null;
private List<NoteBean> lists = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (ListView) findViewById(R.id.lv1); NoteDao dao = new NoteDao(this);
lists = dao.findAll(); // 设置listview的数据了.
// 采用 mvc的方式 设计listview控件
// model 数据
// view listview的控件
// control 控制 //Adapter一: BaseAdapter, 可以自己通过xml布局设计view
//lv1.setAdapter(new MyAdapter()); //Adapter二: ArrayAdapter 方便显示单一文本的listview
//lv1.setAdapter(new ArrayAdapter<String>
//(this, R.layout.item, R.id.item_name, new String[]{"aaa","bbb","vvv"})); //Adapter三: SimpleAdapter
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); //map中还可以存放item对应的图标ID R.drawable.*
Map<String, Object> item1 = new HashMap<String, Object>();
item1.put("image", R.drawable.image1);
item1.put("id", "1");
item1.put("name", "kevin");
item1.put("money", "1000"); Map<String, Object> item2 = new HashMap<String, Object>();
item2.put("image", R.drawable.image2);
item2.put("id", "2");
item2.put("name", "xiang");
item2.put("money", "2000"); data.add(item1);
data.add(item2); lv1.setAdapter(new SimpleAdapter(this, data, R.layout.item,
new String[]{"image","id","name","money"},
new int[]{R.id.image,R.id.item_id,R.id.item_name,R.id.item_money})); } private class MyAdapter extends BaseAdapter{ /**
* 控制listview里面有多个条目.
*/
@Override
public int getCount() {
return lists.size();
} /**
* 返回每个位置对应的view对象
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) { View v = View.inflate(MainActivity.this, R.layout.item, null);
TextView id = (TextView) v.findViewById(R.id.item_id);
id.setText(lists.get(position).getId()+"");
TextView name = (TextView) v.findViewById(R.id.item_name);
name.setText(lists.get(position).getName());
TextView money = (TextView) v.findViewById(R.id.item_money);
money.setText(lists.get(position).getMoney() + ""); return v;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
}

activity_main.xml, activity布局

<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/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"> </ListView> </RelativeLayout>

item.xml, listview需要的view布局

<?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"
android:orientation="horizontal" > <ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/> <TextView
android:id="@+id/item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/item_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>