ListView

本文详细介绍并展示了三种使用 Android ListView 控件的方法:使用 ArrayAdapter 显示简单字符串数组;使用 SimpleAdapter 自定义复杂列表项;以及通过自定义 Adapter 实现更高级的交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".MainActivity">
    <ListView
        android:id="@+id/id_listview_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>
private static final String[] strDatas = new String[] {
        "first", "second", "third", "fourth", "fifth"
        };


ListView lv = (ListView) findViewById(R.id.id_listview_list);
lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, strDatas));

 第二种:可以自定义界面

// item 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/sai_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textSize="18dp"
        android:layout_weight="1"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/sai_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_weight="3"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/sai_phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_weight="4"
        android:textColor="#000"
        />
 
</LinearLayout>
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
Map<String,Object> map1=new HashMap<String, Object>();
map1.put("id", "001");
map1.put("name", "张三");
map1.put("phone", "13588551201");
 
Map<String,Object> map2=new HashMap<String, Object>();
map2.put("id", "002");
map2.put("name", "李四");
map2.put("phone", "13588551202");
 
Map<String,Object> map3=new HashMap<String, Object>();
map3.put("id", "003");
map3.put("name", "张二虎");
map3.put("phone", "13588551203");
 
Map<String,Object> map4=new HashMap<String, Object>();
map4.put("id", "004");
map4.put("name", "李老大");
map4.put("phone", "13588551204");
 
Map<String,Object> map5=new HashMap<String, Object>();
map5.put("id", "005");
map5.put("name", "王老二");
map5.put("phone", "13588551205");
 
data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5);




ListView lv = (ListView) findViewById(R.id.id_listview_list);
         lv.setAdapter(new SimpleAdapter(this,
        	data, R.layout.simple_adapter_item,
        	new String[]{"id","name","phone"},
        	new int[]{R.id.sai_id,R.id.sai_name, R.id.sai_phone}));

 

第三种 

<?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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_light_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="25dp"
        android:text="壁灯"
        android:textColor="#333"
        android:textSize="18sp" />

    <ToggleButton
        android:id="@+id/toggle_light_icon"
        android:layout_width="40dp"
        android:layout_height="22dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/toggle_btn"
        android:checked="true"
        android:focusable="false"
        android:padding="5dp"
        android:text=""
        android:textOff=""
        android:textOn="" />

</LinearLayout>
package com.welldone.home.familywidsom.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.welldone.home.R;
import com.welldone.home.familywidsom.adapter.bean.LightState;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LightAdapter extends BaseAdapter {

    private List<LightState> lights;
    private Context context;

    public LightAdapter(List<LightState> lights, Context context) {
        this.lights = lights;
        this.context = context;
    }

    @Override
    public int getCount() {
        return lights.size();
    }

    @Override
    public Object getItem(int position) {
        return lights.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_wisdom_light_lv, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder.tv_lightname.setText(lights.get(position).getLightName());
        String lightState = lights.get(position).getLightState();
        if (TextUtils.equals("1", lightState)) {
            // 开
            holder.tb_lightstate.setChecked(true);
        } else if (TextUtils.equals("0", lightState)) {
            // 关
            holder.tb_lightstate.setChecked(false);
        }
        holder.tb_lightstate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.tb_lightstate.isChecked()) {
                    //表示从未选中状态转为选中状态
                    Log.e("LeftLightFragment", "未选中 -》 选中");
                } else {
                    //表示是从选中状态,变成未选中状态
                    Log.e("LeftLightFragment", "未选中 《- 选中");
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_light_name)
        TextView tv_lightname;

        @BindView(R.id.toggle_light_icon)
        ToggleButton tb_lightstate;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
        List<LightState> lights = new ArrayList<>();
        lights.add(new LightState("灯光1","1"));
        lights.add(new LightState("灯光2","1"));
        lights.add(new LightState("灯光3","0"));
        lv_lights.setAdapter(new LightAdapter(lights,getActivity()));

 

 

极化码(Polar Code)是由土耳其科学家Erdal Arıkan在2009年提出的一种新型纠错编码技术。它通过利用信道的极化现象,将虚拟信道分为误码率接近0和接近1/2的两类。在编码设计中,数据被放置在误码率极低的信道上,从而实现高效的数据传输。极化码的主要优势在于其理论编码容量能够达到香农限,并且构造方法较为简单。 MATLAB是一种功能强大的数学计算和编程工具,广泛应用于科学研究和工程领域。在极化码的研究中,MATLAB可用于构建编码和解码算法,模拟数据在不同信道条件下的传输效果,验证理论性能,并优化相关参数。 SC(Successive Cancellation,逐位取消)译码是极化码的基本解码方法。它从最可靠的比特开始,依次解码每个虚拟信道,且每个比特的解码结果会影响后续比特的解码,因为它们之间存在依赖关系。虽然SC译码的实现较为简单,但其计算复杂度较高,随着码长的增加,解码时间会线性增长。 SCL(Successive Cancellation List,逐位取消列表)译码是SC译码的改进版本。它通过引入列表机制,同时处理多个路径,从而增强了错误校正能力,并在一定程度上降低了错误率。与SC译码相比,SCL译码虽然需要消耗更多的计算资源,但能够提供更好的性能。 一个完整的MATLAB仿真资源通常包含以下内容: 编码模块:用于实现极化码的生成,包括码字构造和极化矩阵操作等。 信道模型:用于模拟各种通信信道,例如AWGN(加性高斯白噪声)信道或衰落信道。 SC/SCL译码模块:包含SC译码和SCL译码的算法实现。 误码率(BER)计算:通过比较发送和接收的码字,计算误码率,以评估编码性能。 性能曲线绘制:绘制误码率与信噪比(SNR)之间的关系曲线,展示不同译码策略的性能差异。 使用说明:指导用户如何运行仿真,理解代码结构,以及如何调整参数以进行自定义实验。 代码注
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值