工作记录(只是简单的记录下)

12-25:工作需求 需要做带多选功能的diglog--实现

//损益时间diglog
private void lossTimeDigLog(List<String> years){
    AlertDialog.Builder builder = new AlertDialog.Builder(ExpenditureDetailAct.this);
    LayoutInflater inflater = LayoutInflater.from(ExpenditureDetailAct.this);
    View v = inflater.inflate(R.layout.losstime_dig, null);
    final LossTimesAdap adap = new LossTimesAdap(ExpenditureDetailAct.this,years);
    TextView queding = v.findViewById(R.id.queding);
    TextView quxiao = v.findViewById(R.id.quxiao);
    ListView lv1 = v.findViewById(R.id.lv1);
    lv1.setDivider(null);
    lv1.setAdapter(adap);
    final Dialog dialog = builder.create();
    dialog.show();
    dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
    queding.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            List<String> list = adap.getMap();
            String tt="";
            for(String ii:list){
                tt=tt+ii+",";
            }
            if(tt.endsWith(",")){
                tt=tt.substring(0,tt.length()-1);
            }
            times = list;
            sunyiyear_tv.setText(tt);
            dialog.dismiss();
        }
    });
    quxiao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
}

adapter

public class LossTimesAdap extends BaseAdapter {
    
    private Context context;
    private List<String> times;
    HashMap<String,Boolean> map=new HashMap<>();

    public LossTimesAdap(Context context, List<String> times) {
        this.context = context;
        this.times = times;
        for (int i=0;i<times.size();i++){
            map.put(times.get(i),false);
        }
    }

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

    @Override
    public Object getItem(int i) {
        return times.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    
    static class ViewHolder{
        public TextView t01;
        public CheckBox cc;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final String s = times.get(i);
        LossTimesAdap.ViewHolder ViewHolder=null;
        if(view==null){
            ViewHolder=new LossTimesAdap.ViewHolder();
            view= LayoutInflater.from(context).inflate(R.layout.item_lossset_diglog,viewGroup,false);
            ViewHolder.t01=view.findViewById(R.id.t01);
            ViewHolder.cc=view.findViewById(R.id.fufub);
            view.setTag(ViewHolder);
            AutoUtils.autoSize(view);
        }else {
            ViewHolder = (LossTimesAdap.ViewHolder) view.getTag();
        }
        ViewHolder.t01.setText(s.toString());
        final LossTimesAdap.ViewHolder finalViewHolder = ViewHolder;
        ViewHolder.cc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    finalViewHolder.cc.setChecked(true);
                    map.put(s,true);
                }else {
                    finalViewHolder.cc.setChecked(false);
                    map.put(s,false);
                }
            }
        });
        if(map.get(s)){
            ViewHolder.cc.setChecked(true);
        }else {
            ViewHolder.cc.setChecked(false);
        }
        return view;
    }

    public List<String> getMap(){
        List<String> list=new ArrayList<>();
        Set<Map.Entry<String, Boolean>> entries = map.entrySet();
        for(Map.Entry<String, Boolean> ss:entries){
            if(ss.getValue()==true){
                list.add(ss.getKey());
            }
        }
        return list;
    }
}

diglog布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/chaosongban"
        android:textSize="28px"
        android:textColor="#333333"
        android:text="请选择损益时间"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



    <ListView
        android:id="@+id/lv1"
        android:layout_below="@id/chaosongban"
        android:layout_width="400px"
        android:layout_height="300px"></ListView>

    <TextView
        android:id="@+id/queding"
        android:text="确定"
        android:layout_marginTop="50px"
        android:layout_marginLeft="340px"
        android:textSize="28px"
        android:layout_below="@id/lv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/quxiao"
        android:text="取消"
        android:layout_marginTop="50px"
        android:layout_marginLeft="250px"
        android:textSize="28px"
        android:layout_below="@id/lv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />




</RelativeLayout>

adapter布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_marginBottom="15px"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40px">

        <CheckBox
            android:layout_centerVertical="true"
            android:layout_below="@id/fufu"
            android:button="@null"
            android:id="@+id/fufub"
            android:layout_width="30px"
            android:layout_marginLeft="10px"
            android:layout_height="30px"
            android:background="@drawable/checkstyle"
            />

        <TextView
            android:layout_marginLeft="10px"
            android:layout_toRightOf="@id/fufub"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:id="@+id/t01"
            android:text="111"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值