listview的理解——图片和文字的显示

ListView组件用于以列表的形式显示数据,ListView组件采用MVC模式将前端显示与后端数据进行分离,也就是说,ListView组件在装载数据时并不是直接使用ListView类的add或类似的方法添加数据,而是需要指定一个adapter对象。该对象相当于MVC中的C(控制器,Controller),ListView相当于MVC模式中的V(试图,View),用于显示数据。为ListView提供数据的List或数据相当于MVC模式中的M(模型,model);在ListView中组件中通过控制器(adapter对象)获得需要显示的数据,在创建adapter对象时需要指定要显示的数据(List或数组对象),因此,要显示的数据与ListView之间通过adapter对象进行连接,同时又互相独立,也就是说,ListView只知道显示数据来自adapter,并不知道这些数据是来自list还是数组,对于数据来说,只知道将这些数据添加到adapter对象中,并不知道这些数据会被用于ListView组件或其他组件。
********************************在main.xml文件中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/mainbg"
>

<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="@drawable/bg"
android:layout_marginLeft="40dp"
android:layout_marginTop="90dp"
>
</ListView>

</LinearLayout>

*****************************main _item中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >


<ImageView
android:id="@+id/picture"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="65dp"

/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
/>



</LinearLayout>


************************************在java代码中*******************************************
package com.bawei.layout;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class LayoutActivity extends Activity {
private ListView mlistview;
private SimpleAdapter sa;
private ArrayList<HashMap<String,Object>> al;
//获得drawable里的图片,用int型数组
private int[] picture = new int[] {R.drawable.player,R.drawable.media,R.drawable.video, R.drawable.net,R.drawable.app,R.drawable.settings
};
//文字展示,用string型数组
private String[] text= new String[]{"网络播放","多媒体中心","互联网视屏","网上冲浪","应用程序","系统设置"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mlistview = (ListView) findViewById(R.id.listview);
al = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
int length = picture.length;
//for循环,用下标将图片和文字添加到集合里
for(int i= 0;i<length;i++){
hm = new HashMap<String,Object>();
hm.put("picture", picture[i]);
hm.put("text", text[i]);
al.add(hm);
}
//添加到适配器里
sa=new SimpleAdapter(this, al, R.layout.main_listview, new String[]{"picture","text"}, new int[]{R.id.picture,R.id.text});
mlistview.setAdapter(sa);
mlistview.setDividerHeight(0);//listview分割线消失
}
}


大概的流程就是这样子哦~~~~~~~~~~~~~~~~~~~~~
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值