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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章