android中简单的listview代码

1.主界面布局xml代码
<?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="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#339933"
        android:gravity="center"
        android:text="配置设备"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <ListView
        android:id="@+id/smart_control_lamp_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>
2.SceneMatchSwitchActivity界面代码
public class SceneMatchSwitchActivity extends Activity {

    private ListView    mLampListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 加载布局
        setContentView(R.layout.activity_scene_match_switches);
        mLampListview = (ListView) findViewById(R.id.smart_control_lamp_listview);
        // listview绑定Adapter
        mLampListview.setAdapter(new myAdapter());

        // 获取服务器反馈回来的设备类型
    }

    private class myAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // 返回listview的item的总数
            return 7;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                // 通过布局创建listview的item
                convertView = View.inflate(getApplicationContext(), R.layout.item_bluetooth_device_status, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
                // 给holder找view
                holder.mTextViewDeviceName = (TextView) convertView.findViewById(R.id.bluetooth_device_name_textview);
                holder.mTextViewDeviceState = (TextView) convertView.findViewById(R.id.bluetooth_device_state);
                holder.mTextViewDeviceSwitch = (ImageView) convertView.findViewById(R.id.bluetooth_device_switch_imageview);
                holder.mTextViewDeviceIcon = (ImageView) convertView.findViewById(R.id.bluetooth_device_icon_imageview);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // 给控件设置数据
            // mTextViewPosition.setText("位置   " + position);
            // mTextViewName.setText("姓名   " + position);
            return convertView;
        }

        // holder把item的控件变量集合起来,避免每次加载都再找一次id
        private class ViewHolder {
            TextView    mTextViewDeviceName;    // 设备名字
            TextView    mTextViewDeviceState;   // 设备状态
            ImageView   mTextViewDeviceSwitch;  // 设备打开或者关闭
            ImageView   mTextViewDeviceIcon;    // 设备图片
        }
    }
}
3.listview要加载的item样式:图片以及布局代码

这里写图片描述

  • 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/bluetooth_device_switch_imageview"
        style="@style/HotelModeSelect_001"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="30dp"
        android:src="@drawable/openguan" />

    <ImageView
        android:id="@+id/bluetooth_device_icon_imageview"
        style="@style/HotelModeSelect_001"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="9dp"
        android:src="@drawable/lock_close_icon" />

    <LinearLayout
        style="@style/HotelModeSelect_001"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/bluetooth_device_icon_imageview"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/bluetooth_device_name_textview"
            style="@style/HotelModeSelect_001"
            android:textColor="#000000"
            android:text="门锁" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp" >

            <TextView
                style="@style/HotelModeSelect_001"
                android:text="状态:"
                android:textColor="#66000000"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/bluetooth_device_state"
                style="@style/HotelModeSelect_001"
                android:text="未上锁"
                android:textColor="#66000000"
                android:textSize="10sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值