Android Open Source - PowerGridCompanion Power Plant Adapter






From Project

Back to project page PowerGridCompanion.

License

The source code is released under:

Apache License

If you think the Android project PowerGridCompanion listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mintcode.kris.powergridhelper.Adapters;
/*from   ww w.ja va2s  .co  m*/
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.mintcode.kris.powergridhelper.Models.Player;
import com.mintcode.kris.powergridhelper.R;

import java.util.ArrayList;
import java.util.Collections;

import static android.view.View.GONE;

/**
 * Created by kris on 7/1/14.
 */
public class PowerPlantAdapter extends ArrayAdapter<Player> {


    Context mContext;
    ArrayList<Player> list;

    public PowerPlantAdapter(Context context, int resource, ArrayList<Player> l) {
        //super(context, l.size());
        super(context, resource, l);
        mContext = context;
        list = l;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.sample_player_view, parent, false);
        }

        view.findViewById(R.id.AuctionLayout).setVisibility(View.VISIBLE);


        ((TextView) view.findViewById(R.id.AuctionLayout).findViewById(R.id.AuctionMoney)).setText("$" + list.get(position).getMoney());

        TextView name = (TextView) view.findViewById(R.id.EditPlayerName);
        ImageView color = (ImageView) view.findViewById(R.id.PlayerColor);
        final Button buy = (Button) view.findViewById(R.id.AuctionBuy);
        final Button pass = (Button) view.findViewById(R.id.AuctionPass);

        name.setText(list.get(position).getName());
        color.setBackgroundColor(list.get(position).getColor());


        if (list.get(position).getCities_owned() == 0)
            pass.setVisibility(GONE);

        pass.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                buy.setVisibility(View.GONE);
                pass.setVisibility(View.GONE);
                notifyDataSetChanged();
            }
        });

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                final MutableInteger cost = new MutableInteger(0);

                LayoutInflater inflater = LayoutInflater.from(mContext);
                final View dialoglayout = inflater.inflate(R.layout.power_plant_auction_buy_dialog, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
                alertDialogBuilder.setView(dialoglayout);

                Button minusTen = (Button) dialoglayout.findViewById(R.id.MinusTen);
                Button minusFive = (Button) dialoglayout.findViewById(R.id.MinusFive);
                Button minusOne = (Button) dialoglayout.findViewById(R.id.MinusOne);
                Button plusTen = (Button) dialoglayout.findViewById(R.id.PlusTen);
                Button plusFive = (Button) dialoglayout.findViewById(R.id.PlusFive);
                Button plusOne = (Button) dialoglayout.findViewById(R.id.PlusOne);

                final TextView total = (TextView) dialoglayout.findViewById(R.id.DialogMoneyAmount);
                final EditText id = (EditText) dialoglayout.findViewById(R.id.DialogPlantID);

                total.setText("$" + cost.getValue());

                //Calc Logic
                minusTen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() - 10) >= 0){
                            cost.setValue(cost.getValue() - 10);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });

                minusFive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() - 5) >= 0){
                            cost.setValue(cost.getValue() - 5);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });

                minusOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() - 1) >= 0){
                            cost.setValue(cost.getValue() - 1);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });

                plusTen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() + 10) <= list.get(position).getMoney()) {
                            cost.setValue(cost.getValue() + 10);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });

                plusFive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() + 5) <= list.get(position).getMoney()) {
                            cost.setValue(cost.getValue() + 5);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });

                plusOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if((cost.getValue() + 1) <= list.get(position).getMoney()) {
                            cost.setValue(cost.getValue() + 1);
                            total.setText("$" + cost.getValue());
                        }
                    }
                });


                //set title
                alertDialogBuilder.setTitle("Purchase Power Plant");


                //set buttons
                alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                alertDialogBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (!id.getText().equals("")) {
                            list.get(position).setMoney(list.get(position).getMoney() - cost.getValue());
                            if(list.get(position).getLargest_power_plant() < Integer.getInteger(id.getText().toString(),0)){
                                list.get(position).setLargest_power_plant(Integer.getInteger(id.getText().toString(),0));
                            }

                            buy.setVisibility(GONE);
                            pass.setVisibility(View.GONE);
                            notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();


            }
        });


        return view;
    }


    public class MutableInteger {
        private int value;
        public MutableInteger(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }
        public void setValue(int value) {
            this.value = value;
        }
    }

}




Java Source Code List

com.mintcode.kris.powergridhelper.Activities.BuildingActivity.java
com.mintcode.kris.powergridhelper.Activities.PlayerSelection.java
com.mintcode.kris.powergridhelper.Activities.PlayerSetup.java
com.mintcode.kris.powergridhelper.Activities.PowerPlantAuctionActivity.java
com.mintcode.kris.powergridhelper.Activities.ResourcePurchaseActivity.java
com.mintcode.kris.powergridhelper.Activities.StartScreen.java
com.mintcode.kris.powergridhelper.Activities.TurnOrderSummary.java
com.mintcode.kris.powergridhelper.Adapters.BuildingAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PlayerOrderAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PlayerSetupAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PowerPlantAdapter.java
com.mintcode.kris.powergridhelper.Adapters.ResourceAdapter.java
com.mintcode.kris.powergridhelper.Models.Player.java