1. 程式人生 > >兩個ListView實現的Android點餐頁面

兩個ListView實現的Android點餐頁面

package com.example.henry.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.henry.test.adapter.CartAdapter;
import com.example.henry.test.adapter.GoodsClassifyAdapter;
import com.example.henry.test.adapter.GoodsDetailsAdapter;
import com.example.henry.test.app.Constant;
import com.example.henry.test.entity.GoodsClassify;
import com.example.henry.test.entity.GoodsDetails;
import com.example.henry.test.function.AppUtility;
import com.example.henry.test.function.JsonUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private ListView mParentList;
    private ListView mChildList;
    private TextView mBill;
    private TextView mPrice;
    private TextView mCount;
    private ImageView mCart;
    private LinearLayout mWindow;
    private ListView mCartList;
    private TextView mClear;
    private View mView;

    private Boolean isShow = false;
    private List<GoodsClassify> classifys = new ArrayList<>();
    private List<GoodsDetails> detailses = new ArrayList<>();
    private List<GoodsDetails> list ;//  購物車中商品列表

    private CartAdapter cartAdapter;
    private GoodsClassifyAdapter classifyAdapter;
    private GoodsDetailsAdapter detailsAdapter;
    private static DecimalFormat df = new DecimalFormat("0.00");
    private int location = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        setContentView(R.layout.activity_main);
        init();
        getGoodsData();

    }

    /**
     * 初始化控制元件,並設定監聽事件
     */
    public void init(){
        mParentList = (ListView)findViewById(R.id.parentlist);
        mChildList = (ListView) findViewById(R.id.childlist);
        mCount = (TextView) findViewById(R.id.tv_count);
        mBill = (TextView) findViewById(R.id.billing);
        mPrice = (TextView) findViewById(R.id.price_main);
        mCart = (ImageView) findViewById(R.id.cart);
        mWindow = (LinearLayout) findViewById(R.id.window);
        mCartList = (ListView) findViewById(R.id.list_cart);
        mClear = (TextView) findViewById(R.id.clear);
        mView = (View) findViewById(R.id.view);
        mBill.setOnClickListener(this);
        mCart.setOnClickListener(this);
        mClear.setOnClickListener(this);
        mView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.billing:
                isShow = setWindowView(true);
                Intent intent = new Intent(this,BillActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("LIST", (Serializable) list);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.cart:
                isShow = setWindowView(isShow);
                createCartList();
                cartAdapter = new CartAdapter(MainActivity.this,list);
                mCartList.setAdapter(cartAdapter);
                break;
            case R.id.clear:
                clearList();
                break;
            case R.id.view:
                isShow = setWindowView(true);
                break;
            default:
                break;
        }
    }

    /*
     *清空列表
     */
    public void clearList(){
        for (int i=0;i<classifys.size();i++) {
            for (int j = 0; j < classifys.get(i).getProdList().size(); j++) {
                classifys.get(i).getProdList().get(j).setProdCount(0);
            }
        }
        createCartList();
        cartAdapter = new CartAdapter(MainActivity.this,list);
        mCartList.setAdapter(cartAdapter);
        updateList(true,true,false);

        mPrice.setText("¥" + df.format(0.00) + "元" );
        mCount.setVisibility(View.GONE);
    }

    /**
     * 重新整理list,
     * @param flag  表示adapter,0代表classifyAdapter;1代表detailsAdapter;2代表cartAdapter;
     */
    public void updateList(Boolean... flag){
        if (flag[0])
            classifyAdapter.notifyDataSetChanged();
        if (flag[1])
            detailsAdapter.notifyDataSetChanged();
        if (flag[2])
            cartAdapter.notifyDataSetChanged();
    }

    /**
     * 修改購物車顯示狀態,並返回修改後的狀態
     * @param isShow 購物車詳細資訊是否顯示
     * @return      購物車顯示的當前狀態  顯示true,不顯示為false
     */
    public Boolean setWindowView(Boolean isShow){
        if (isShow == false){
            mView.setVisibility(View.VISIBLE);
            mWindow.setVisibility(View.VISIBLE);

            isShow = true;
        }else {
            mView.setVisibility(View.GONE);
            mWindow.setVisibility(View.GONE);
            updateList(true,true,false);
            isShow = false;
        }
        return isShow;
    }

    /**
     * 初始化列表
     */
    public void initView(){
        if(classifys != null &&  classifys.size() != 0){
            //設定parentList 資料
            classifyAdapter = new GoodsClassifyAdapter(getApplicationContext(),classifys);
            mParentList.setAdapter(classifyAdapter);

            //設定childList 資料
            detailses.addAll(classifys.get(0).getProdList());
            detailsAdapter = new GoodsDetailsAdapter(MainActivity.this,detailses,classifyAdapter);
            mChildList.setAdapter(detailsAdapter);
        }
        mParentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                location = position;
                classifyAdapter.clearSelection(position);
                detailses.clear();
                detailses.addAll(classifys.get(position).getProdList());
                updateList(false,true,false);
            }
        });
    }


    /**
     * 計算總價與選中的個數,並顯示在介面上
     */
    public void calculateTotalPrice(){
        createCartList();
        int num = 0;
        double price = 0.00;
        for (int i=0;i<list.size();i++){
            num += list.get(i).getProdCount();
            price += Double.parseDouble(list.get(i).getProdPrice()) * list.get(i).getProdCount();
        }

        mPrice.setText("¥" + df.format(price) + "元" );
        if (num == 0){
            this.mCount.setVisibility(View.GONE);
        }else{
            this.mCount.setVisibility(View.VISIBLE);
            this.mCount.setText(num + "");
        }

    }

    /**
     * 建立購物車商品列表
     */
    public void createCartList(){
        list = new ArrayList<>();
        for (int i=1;i<classifys.size();i++){
            for (int j=0;j<classifys.get(i).getProdList().size();j++){
                if (classifys.get(i).getProdList().get(j).getProdCount() > 0){
                    list.add(classifys.get(i).getProdList().get(j));
                }
            }
        }
    }

    /**
     * 修改指定id的商品的數量
     * @param id
     * @param num
     */
    public void modifyGoodsNum(String id,int num){
        for (int i=0;i<classifys.size();i++){
            for (int j=0;j<classifys.get(i).getProdList().size();j++){
                if (id.equals(classifys.get(i).getProdList().get(j).getProdId())){
                    classifys.get(i).getProdList().get(j).setProdCount(num);

                }
            }
        }
    }


    /**
     * 從伺服器獲取商品資料
     */
    private void getGoodsData() {
        RequestParams params = new RequestParams();


        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Constant.URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject result = new JSONObject(responseInfo.result);
                    if (AppUtility.getStatus(getApplicationContext(), result)) {
                        classifys = JsonUtils.getGoodsList(result);
                        initView();
                    } else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}


以上是程式碼的主體部分,MainActivity與3個介面卡之間的相互呼叫,從而實現資料的同步。不怎麼會講,光看一個也看不懂,下面我會附上原始碼地址。希望大神門批評指正。