适配器Adapter
ArrayAdapter 只能为纯文字
SimpleAdapter 只能接受list类型的数据,并且数据只能为Map类型,可为图文
BaseAdapter 抽象类 需要用子类继承
子类重写BaseAdapter的方法
构造函数(Context context; List<T> list)
int getCount() 总数据量
Object geItem(int position) 根据position得到某一行
long getItemId() 获得某一行的id
View getView(int position,View convertView,ViewGroup parent){}
package com.example.administrator.myapplication; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by Administrator on 2016-8-26. */ public class NewListActivity extends Activity { /*重写父类OnCreat方法 ALT+Insert*/ ListView listView; List<Map<String,Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newlistviewlayout);/*设置关联视图 连接起来*/ /* listView = (ListView) findViewById(R.id.myList); *//*适配器 泛型<String> 1类型:NewListActivity.this 2数据 样式 3放的数据*//* ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( NewListActivity.this, android.R.layout.simple_list_item_1, new String[]{"雷喜逢","卓翔","陈传印"}); listView.setAdapter(arrayAdapter);*/ listView = (ListView) findViewById(R.id.myList); list = new ArrayList<Map<String, Object>>(); Map<String,Object> new1 = new HashMap<String, Object>(); Map<String,Object> new2 = new HashMap<String, Object>(); Map<String,Object> new3 = new HashMap<String, Object>(); new1.put("title","陈传印出任国家主席!"); new1.put("content","毛主席睁眼啦!"); new1.put("img",R.drawable.asd); new2.put("title","我的祖国!"); new2.put("content","毛主席日本一日游!"); new2.put("img",R.drawable.asd); new3.put("title","我玩游戏啦!"); new3.put("content","开学啦!"); new3.put("img",R.drawable.asd); list.add(new1); list.add(new2); list.add(new3); SimpleAdapter simpleAdapter = new SimpleAdapter( NewListActivity.this, /*1类型:NewListActivity.this*/ list, /*2 数据 list*/ R.layout.newlistitem, /*3 样式 */ new String[]{"title","content","img"}, /*4从哪开始from*/ new int[]{R.id.title, R.id.content, R.id.img}); /*5 到哪结束to*/ listView.setAdapter(simpleAdapter); } }
<?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:layout_width="60dp" android:layout_height="60dp" android:id="@+id/img"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#f00" android:textSize="20sp"/> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/> </LinearLayout> </LinearLayout>
<?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"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myList"> </ListView> </LinearLayout>