为ListView添加适配器,使list可以显示一个ImageView和两个textview

来源:互联网 发布:sql replace 正则 编辑:程序博客网 时间:2024/05/01 21:49

首先定义主Actvity的显示布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ListView 
        android:id="@+id/mylist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></ListView>
</LinearLayout>

添加到列表中的每个元素都叫item,为每个item定义布局格式文件:simple_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <ImageView 
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp"     
        />
    <LinearLayout  android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:paddingLeft="10dp"
            />
        <TextView 
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:paddingLeft="10dp"
            />
    </LinearLayout>
</LinearLayout>


定义主testActivity,并添加监听器

public class SimpleAdapterTest extends Activity{
//测试列表适配器,列表中的每一项都是图像和两个textview
private String[] names=new String[]{"虎头","弄玉","李清照","李白"};
private String[] descs=new String[]{"可爱的女孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"};
private int[] imageIds=new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem=new HashMap<String,Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItem.put("desc",descs[i]);
listItems.add(listItem);
}

SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.simple_item, new String[]{"personName","header","desc"}, new int[]{R.id.name,R.id.header,R.id.desc});
ListView list=(ListView)findViewById(R.id.mylist);
list.setAdapter(simpleAdapter);
//list.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position,
// long id) {
//
// Toast.makeText(SimpleAdapterTest.this, names[position]+"被点击了", Toast.LENGTH_LONG).show();
// }
//});
list.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//Toast.makeText(SimpleAdapterTest.this, names[position]+"被选中",Toast.LENGTH_LONG).show();
System.out.println(names[position]+"被选中。。。。。");
}


@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
});
}
}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去医院看病没带身份证怎么办 ps4星战2鬼服怎么办 冒险岛英雄五转怎么办 6儿童视力低常怎么办 腰干活累的酸痛怎么办 狗狗体力很差怎么办啊 脉差总是五十多怎么办 吃过敏药嗜睡乏力怎么办 写字紧张心跳的快手抖怎么办 怀孕食欲差没精神怎么办 爬个三楼就喘了怎么办 头被篮球砸了怎么办 打球手指肿了很久怎么办 客人吃饭跑单了怎么办 脑子总是记不住东西怎么办 穿的鞋有味道怎么办 生完孩子血糖高怎么办 生了孩子子宫大怎么办 生完孩子阴吹怎么办 怀孕初期感冒嗓子发炎怎么办 脑子转不过来弯怎么办 他说我道行不深怎么办 鞋子内测磨破了怎么办 老公拉肚子拉的很厉害怎么办 老公拉肚子拉得很厉害怎么办 和老公吵架很厉害怎么办 新鞋鞋底吱吱响怎么办 新鞋鞋底滑了怎么办 工作中同事不配合怎么办 做体力活一身疼怎么办 大脑左则中动脉血管狭窄?怎么办 儿童体质差免疫力差怎么办 nba篮球大师体力不够怎么办 一千米差10秒怎么办 四十岁以后体力和耐力差怎么办 打篮球一打比赛就紧张怎么办 耐克空军鞋鞋底边胶掉了怎么办 鞋底踩到老鼠胶怎么办 头蒙怎么办最快最有效 备孕老公压力大怎么办 宝宝太兴奋不睡怎么办