zoukankan      html  css  js  c++  java
  • 全选,反选,全不选以及计算价格,删除

    代码如下:实现购物车类似的功能,分为两个class,两个xml:

    activity_main.xml:

    <LinearLayout 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"
        android:orientation="vertical" >
      <LinearLayout 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          >
        <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            android:layout_weight="1"
            />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反 选" />
    
        <Button 
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全不选"
             android:layout_weight="1"
            />
    </LinearLayout>
    <TextView 
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ListView 
        android:id="@+id/listveiw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        ></ListView>
      
      
      
      
    </LinearLayout>

    MainActivity.class主页面,重要代码存在处:

    package com.example.day_all_button;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.example.day_all_button.Adper.ViewHolder;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener{
    
         private Button button1;
            private Button button2;
            private Button button3;
            private ListView listView;
            private List<Good> list;
            private TextView price;
            private int num=0;
            private int pric=0;
            private BaseAdapter adapter;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              button1 = (Button) findViewById(R.id.button1);//全选
                button2 = (Button) findViewById(R.id.button2);//反选
                button3 = (Button) findViewById(R.id.button3);//全不选
                listView = (ListView) findViewById(R.id.listveiw);//价格
                price = (TextView) findViewById(R.id.price);
                list = new ArrayList<Good>();
                //赋值
                for (int i = 0; i < 80; i++) {
                    list.add(new Good(i+"", false));
                }
                //适配
                adapter = new Adper(list,MainActivity.this);
                listView.setAdapter(adapter);
                button1.setOnClickListener(this);
                button2.setOnClickListener(this);
                button3.setOnClickListener(this);
                // 绑定listView的监听器 
                listView.setOnItemClickListener(new OnItemClickListener() {
    
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
                        // TODO Auto-generated method stub
                         // 取得ViewHolder对象
                        ViewHolder viewHolder = (ViewHolder) arg1.getTag();
                         // 改变CheckBox的状态  
                        viewHolder.checkBox.toggle();
                         // 将CheckBox的选中状况记录下来  
                        list.get(arg2).setBo(viewHolder.checkBox.isChecked());
                         // 调整选定条目  
                        if (viewHolder.checkBox.isChecked() == true) {  
                            num++;  
                            pric+=Integer.parseInt(list.get(arg2).getName());    
                        } else {  
                            num--;  
                            pric-=Integer.parseInt(list.get(arg2).getName());    
                        } 
                        // 用TextView显示  
                        price.setText("一共选了"+num+"件,"+"价格是"+pric+"");
                    }
                });
                
                
                
        }
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            switch (arg0.getId()) {
            case R.id.button1:
                  num = 0;
                  pric = 0;
    
                 for (int i = 0; i < list.size(); i++) {
                     //改变boolean
                     list.get(i).setBo(true);
                     //如果为选中
                          if(list.get(i).getBo()){
                       
                          num++;
                          pric+=Integer.parseInt(list.get(i).getName());
                     
                     } 
                 }
                 //刷新
                 adapter.notifyDataSetChanged();
                 //显示
                 price.setText("一共选了"+num+"件,"+"价格是"+pric+"");
                 break;
            case R.id.button2:
                   num = 0;
                   pric = 0;
                  for (int i = 0; i < list.size(); i++) {
                      //改值
                      if(list.get(i).getBo()){
                          list.get(i).setBo(false);
                      }else{
                          list.get(i).setBo(true);
                      }
                      //刷新
                      adapter.notifyDataSetChanged();
                      //如果为选中
                      if(list.get(i).getBo()){
                           
                           num++;
                           pric+=Integer.parseInt(list.get(i).getName());
                          
                      } 
                  }
                  // 用TextView显示  
                  price.setText("一共选了"+num+"件,"+"价格是"+pric+"");
                  break;
            case R.id.button3:
                  num = 0;
                  pric = 0;
                 for (int i = 0; i < list.size(); i++) {
                     //改值
                     list.get(i).setBo(false);
                     //刷新
                     adapter.notifyDataSetChanged();
                     //如果为选中
                     if(list.get(i).getBo()){ 
                          num++;
                          pric+=Integer.parseInt(list.get(i).getName());     
                     } 
                     
                 }
                 price.setText("一共选了"+num+"件,"+"价格是"+pric+"");
                break;
            }
              }
                
        
             
    
            
    
            
        
        
    
    }

    需要定义一个vo类,泳衣存放boolean类型的值,判断是否选中:

    package com.example.day_all_button;
    
    public class Good {
        private String name;
        private boolean bo;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public boolean getBo() {
            return bo;
        }
        public void setBo(boolean bo) {
            this.bo = bo;
        }
        public Good(String name, boolean bo) {
            super();
            this.name = name;
            this.bo = bo;
        }
        
        
                
        
            
        
                
    }

    加下来事适配器:

    package com.example.day_all_button;
    
    import java.util.List;
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.TextView;
    
    public class Adper extends BaseAdapter {
        List<Good> list;
        Context context;
        
        public Adper(List<Good> list, Context context) {
            // TODO Auto-generated constructor stub
        this.list=list;
        this.context=context;
        
        
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }
    
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }
    
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ViewHolder viewHolder;
            if(convertView==null){
                convertView=View.inflate(context, R.layout.item, null);
             viewHolder=new ViewHolder();
             viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
            viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox); 
            convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText("价格:"+list.get(position).getName());
            
            //显示checkBox
            viewHolder.checkBox.setChecked(list.get(position).getBo());
            
            return convertView;
            
        }
    class ViewHolder{
        TextView textView;
        CheckBox checkBox;
    }
    }

    适配器的xml:

    <?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" >
       <TextView 
           android:id="@+id/text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="ddd"
           
           />
    
       <CheckBox
           android:id="@+id/checkbox"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
          
            android:focusable="false"  
            android:clickable="false"
    
    
           android:layout_alignParentRight="true"
           android:layout_alignParentTop="true" />
    
    </RelativeLayout>

     删除功能:

        case R.id.delete:
             
                
                //迭代集合对象
                Iterator<User> iterator = list_user.iterator();
                while(iterator.hasNext())
                {
                    
                    User next = iterator.next();
                    boolean ischeck = next.isIscheck();
                    
                    if(ischeck)
                    {
                        iterator.remove();
                        System.out.println(list_user.size());
                    }
                }
  • 相关阅读:
    75. Sort Colors
    101. Symmetric Tree
    121. Best Time to Buy and Sell Stock
    136. Single Number
    104. Maximum Depth of Binary Tree
    70. Climbing Stairs
    64. Minimum Path Sum
    62. Unique Paths
    css知识点3
    css知识点2
  • 原文地址:https://www.cnblogs.com/123p/p/5386652.html
Copyright © 2011-2022 走看看