Android Open Source - PowerGridCompanion Resource 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;
/* w w  w  .j a va 2  s .  co m*/
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

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

import static android.view.View.GONE;

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

    private final ResourcePurchaseActivity callback;
    Context mContext;
    ArrayList<Player> list;
    HashMap<String,Integer> resourceList;

    public ResourceAdapter(Context context, int resource, ArrayList<Player> l, HashMap<String, Integer> rl, ResourcePurchaseActivity resourcePurchaseActivity) {
        super(context, resource,l);
        mContext = context;
        list = l;
        resourceList = rl;
        callback = resourcePurchaseActivity;
    }


    @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);
        view.findViewById(R.id.ResourceSummary).setVisibility(View.VISIBLE);
        view.findViewById(R.id.AuctionPass).setVisibility(View.GONE);

        ((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);

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

        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.resource_dialog, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
                alertDialogBuilder.setView(dialoglayout);

                //set Resource Values
                TextView coalVal = (TextView)dialoglayout.findViewById(R.id.CoalValue);
                TextView OilVal = (TextView)dialoglayout.findViewById(R.id.OilValue);
                TextView GarbageVal = (TextView)dialoglayout.findViewById(R.id.GarbageValue);
                TextView UraniumVal = (TextView)dialoglayout.findViewById(R.id.UraniumValue);

                final TextView costView = (TextView)dialoglayout.findViewById(R.id.ResourceCost);

                coalVal.setText(list.get(position).getCoal()+"");
                OilVal.setText(list.get(position).getOil()+"");
                GarbageVal.setText(list.get(position).getGarbage()+"");
                UraniumVal.setText(list.get(position).getUranium()+"");

                //Buttons
                final MutableInteger coalAdd = new MutableInteger(0);
                final MutableInteger oilAdd = new MutableInteger(0);
                final MutableInteger garbageAdd = new MutableInteger(0);
                final MutableInteger uraniumAdd = new MutableInteger(0);

                final Button coalButton = (Button)dialoglayout.findViewById(R.id.CoalButton);
                final Button oilButton = (Button)dialoglayout.findViewById(R.id.OilButton);
                final Button garbageButton = (Button)dialoglayout.findViewById(R.id.GarbageButton);
                final Button uraniumButton = (Button)dialoglayout.findViewById(R.id.UraniumButton);

                coalButton.setText("Coal:"+coalAdd.getValue());
                oilButton.setText("Oil:"+oilAdd.getValue());
                garbageButton.setText("Garbage:"+garbageAdd.getValue());
                uraniumButton.setText("Uranium:"+uraniumAdd.getValue());

                coalButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(coalAdd.getValue() < resourceList.get("Coal")){

                            //Add a coal
                            coalAdd.setValue(coalAdd.getValue() + 1);
                            //Compute cost
                            cost.setValue(cost.getValue()+(8 - ((resourceList.get("Coal") - coalAdd.getValue()) / 3)));

                            coalButton.setText("Coal:"+coalAdd.getValue());
                            costView.setText("$"+cost.getValue());
                        }
                    }
                });

                oilButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(oilAdd.getValue() < resourceList.get("Oil")){
                            //Add a oil
                            oilAdd.setValue(oilAdd.getValue() + 1);

                            //Compute cost
                            cost.setValue(cost.getValue()+(8 - ((resourceList.get("Oil") - oilAdd.getValue()) / 3)));

                            oilButton.setText("Oil:"+oilAdd.getValue());
                            costView.setText("$"+cost.getValue());
                        }
                    }
                });

                garbageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(garbageAdd.getValue() < resourceList.get("Garbage")){
                            //Add a coal
                            garbageAdd.setValue(garbageAdd.getValue() + 1);

                            //Compute cost
                            cost.setValue(cost.getValue()+(8 - ((resourceList.get("Garbage") - garbageAdd.getValue()) / 3)));

                            garbageButton.setText("Garbage:"+garbageAdd.getValue());
                            costView.setText("$"+cost.getValue());
                        }
                    }
                });

                uraniumButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(uraniumAdd.getValue() < resourceList.get("Uranium")){
                            //Add a coal
                            uraniumAdd.setValue(uraniumAdd.getValue() + 1);

                            //Compute cost
                            //cost.setValue(8 - ((uraniumAdd.getValue() + 1) / 1));
                            if(resourceList.get("Uranium") - uraniumAdd.getValue() < 4){
                                cost.setValue(cost.getValue() + (16 - ((resourceList.get("Uranium") - uraniumAdd.getValue())*2)));

                            }else{
                                cost.setValue(cost.getValue() + (8 - ((resourceList.get("Uranium") - uraniumAdd.getValue()) - 4)));
                            }


                            uraniumButton.setText("Uranium:"+uraniumAdd.getValue());
                            costView.setText("$"+cost.getValue());
                        }
                    }
                });

                Button reset = (Button)dialoglayout.findViewById(R.id.ResetButton);

                reset.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        coalAdd.setValue(0);
                        oilAdd.setValue(0);
                        garbageAdd.setValue(0);
                        uraniumAdd.setValue(0);

                        cost.setValue(0);

                        coalButton.setText("Coal:"+coalAdd.getValue());
                        oilButton.setText("Oil:"+oilAdd.getValue());
                        garbageButton.setText("Garbage:"+garbageAdd.getValue());
                        uraniumButton.setText("Uranium:"+uraniumAdd.getValue());

                        costView.setText("$"+cost.getValue());
                    }
                });


                //set title
                alertDialogBuilder.setTitle(list.get(position).getName()+" $"+list.get(position).getMoney());


                //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(cost.getValue() > list.get(position).getMoney()){
                            Toast.makeText(mContext,"Can Not Afford These Resources!",Toast.LENGTH_LONG).show();
                        }else{
                            list.get(position).setMoney(list.get(position).getMoney() - cost.getValue());
                            list.get(position).setCoal(list.get(position).getCoal() + coalAdd.getValue());
                            list.get(position).setOil(list.get(position).getOil() + oilAdd.getValue());
                            list.get(position).setGarbage(list.get(position).getGarbage() + garbageAdd.getValue());
                            list.get(position).setUranium(list.get(position).getUranium() + uraniumAdd.getValue());
                            resourceList.put("Coal",resourceList.get("Coal") - coalAdd.getValue());
                            resourceList.put("Oil",resourceList.get("Oil") - oilAdd.getValue());
                            resourceList.put("Garbage",resourceList.get("Garbage") - garbageAdd.getValue());
                            resourceList.put("Uranium",resourceList.get("Uranium") - uraniumAdd.getValue());
                            callback.setupResource();
                            notifyDataSetChanged();
                            dialog.cancel();
                        }

                    }
                });

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

                // show it
                alertDialog.show();


            }
        });

        TextView coalCount = (TextView)view.findViewById(R.id.CoalValue);
        TextView oilCount = (TextView)view.findViewById(R.id.OilValue);
        TextView garbageCount = (TextView)view.findViewById(R.id.GarbageValue);
        TextView uraniumCount = (TextView)view.findViewById(R.id.UraniumValue);

        coalCount.setText(list.get(position).getCoal()+"");
        oilCount.setText(list.get(position).getOil()+"");
        garbageCount.setText(list.get(position).getGarbage()+"");
        uraniumCount.setText(list.get(position).getUranium()+"");

        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