解決商品訂單增加,以及以後的清空功能

問題突破:

1.listView條目中有checkbox,要解決checkbox複用問題。

2.要記錄選中的checkbox條目數。


---------------------------activity_main--------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:orientation="horizontal" >
            <CheckBox
                android:id="@+id/main_all_check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:text="全選"
                />
            <TextView
                android:layout_marginLeft="20dp"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content"

                android:text="品名"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"

                android:text="數量"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"

                android:text="金額"
                />
        </LinearLayout>
        <ListView
            android:id="@+id/main_listView_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#eeeeee"
            android:layout_weight="1"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:orientation="horizontal" >
            <LinearLayout
                android:id="@+id/clear_main"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    android:text="清空("
                    />
                <TextView
                    android:id="@+id/clear_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    android:text=")"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#eeeeee"
                android:paddingTop="3dp"
                android:paddingBottom="3dp"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textSize="18sp"
                android:gravity="center"
                android:text="掛單"
                />
        </LinearLayout>

        <Button
            android:id="@+id/add_order"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="添加訂單"
            />
    </LinearLayout>


</LinearLayout>
-----------------------------listview_order-----------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:minHeight="40dp"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/check_option"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:clickable="false"
        />
    <TextView
        android:id="@+id/orderNumber"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/productName"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/number"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/money"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
</LinearLayout>

---------------------------MainActivity---------------------------------

package com.example.a1.d1026checkboxclear;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    private String TAG = "SPMainActivity";
    public static MainActivity mainActivity;

    private ListView listView;
    private LinearLayout clear;

    private TextView clearNumber;
    private CheckBox allCheck;

    private Button addOrder;
    ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();

    public MainActivity() {
        this.mainActivity = this;
    }

    private OrderFromAdapter adapter;
    private boolean flag=true;
    public  static int checkNum=0;
    private static HashMap<Integer,Boolean> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.main_listView_list);
        clear = (LinearLayout) findViewById(R.id.clear_main);
        clearNumber = (TextView) findViewById(R.id.clear_number);
        allCheck = (CheckBox) findViewById(R.id.main_all_check);
        addOrder=(Button)findViewById(R.id.add_order);
        //初始化checkbox
        clearNumber.setText("" + checkNum);
        adapter = new OrderFromAdapter(list, MainActivity.this);
        listView.setAdapter(adapter);
        //選中項修改它的狀態值
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                OrderFromAdapter.ViewHolder viewHolder = (OrderFromAdapter.ViewHolder) view.getTag();
                viewHolder.checkBox.toggle();
                //選中項修改它的狀態值
                OrderFromAdapter.getIsSelected().put(position, viewHolder.checkBox.isChecked());
                // 修改清空數目
                if (viewHolder.checkBox.isChecked() == true) {
                    checkNum++;
                } else {
                    checkNum--;
                }
                clearNumber.setText("" + checkNum);
            }
        });
        //增加訂單
        addOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("CONTENT", "魚香肉絲");
                map.put("NUMBER", ""+1);
                map.put("UNITPRICE", ""+25);
                map.put("PRICE",""+1*25);
                list.add(map);
                OrderFromAdapter.getIsSelected().put(list.size()-1,false);
                dataChanged();
            }
        });
        //全選,全部消除
        allCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //全選,選中個數爲訂單個數
                if (b) {
                    for (int i = 0; i < list.size(); i++) {
                        OrderFromAdapter.getIsSelected().put(i, true);
                    }
                    checkNum = list.size();
                    //刷新,設置選中個數
                    dataChanged();
                } else {//全部取消,選中個數爲0
                    for (int i = 0; i < list.size(); i++) {
                        OrderFromAdapter.getIsSelected().put(i, false);
                    }
                    checkNum = 0;
                    //刷新,設置選中個數
                    dataChanged();
                }
            }
        });
        //清空選中項
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = list.size(); i > 0; i--) {
                    if (OrderFromAdapter.getIsSelected().get(i-1)) {
                        list.remove(i-1);
                    }
                }
                //清空選中項之後,遍歷集合還是多少條目,把剩下的條目相應的checkbox設置爲false
                OrderFromAdapter.getIsSelected().clear();
                for (int i = 0; i < list.size(); i++) {
                    OrderFromAdapter.getIsSelected().put(i, false);
                }
                adapter.notifyDataSetChanged();
                //清空之後清空項變爲0
                checkNum = 0;
                clearNumber.setText("" + checkNum);
            }
        });
    }
    private void dataChanged() {
        // 通知listView刷新
        adapter.notifyDataSetChanged();
        // TextView顯示最新的選中數目
        clearNumber.setText(""+checkNum );
    }


}


----------------------OrderFromAdapter-------------------------------

package com.example.a1.d1026checkboxclear;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by a1 on 2016/10/25.
 */
public class OrderFromAdapter extends BaseAdapter {
    private ArrayList<HashMap<String,String>> list;
    private LayoutInflater inflater;
    //創建一個集合,防止checkbox複用發生錯亂
    private static HashMap<Integer,Boolean> isSelected;
    private Context context;

    public OrderFromAdapter(ArrayList<HashMap<String,String>> list,Context context){
        this.context = context;
        this.list=list;
        inflater=LayoutInflater.from(context);
        isSelected = new HashMap<Integer, Boolean>();
        initDate();
        Log.v("TAG",isSelected.toString());
    }

    // 初始化isSelected的數據
    private void initDate(){
        for(int i=0; i<list.size();i++) {
            getIsSelected().put(i,false);
        }
    }

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

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

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

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        if(view==null){
            ViewHolder viewHolder=new ViewHolder();
            view=inflater.inflate(R.layout.listview_order,null);
            viewHolder.checkBox=(CheckBox) view.findViewById(R.id.check_option);
            viewHolder.orderNumber=(TextView) view.findViewById(R.id.orderNumber);
            viewHolder.textView1=(TextView) view.findViewById(R.id.productName);
            viewHolder.textView2=(TextView) view.findViewById(R.id.number);
            viewHolder.textView3=(TextView) view.findViewById(R.id.money);
            view.setTag(viewHolder);
        }
        ViewHolder viewHolder=(ViewHolder)view.getTag();
        HashMap<String, String> map =list.get(position);
        //點擊複選框,存儲相應複選框的狀態
        Log.v("TAG",isSelected.toString());
        //因爲添加訂單,判斷是否有數據,防止空指針異常
        if(getIsSelected().size()!=0){
            viewHolder.checkBox.setChecked(getIsSelected().get(position));
        }
        if(position<9){
            viewHolder.orderNumber.setText("0"+(position+1));
        }else{
            viewHolder.orderNumber.setText(""+(position+1));
        }
        viewHolder.textView1.setText( map.get("CONTENT"));
        viewHolder.textView2.setText( map.get("NUMBER"));
        viewHolder.textView3.setText( map.get("PRICE"));
        return view;
    }
    public static HashMap<Integer,Boolean> getIsSelected() {
        return isSelected;
    }
    public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
        OrderFromAdapter.isSelected = isSelected;
    }
    public class ViewHolder{
        public CheckBox checkBox;
        public TextView orderNumber,textView1,textView2,textView3;
    }
}

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