Back to project page PowerGridCompanion.
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.
package com.mintcode.kris.powergridhelper.Adapters; //w w w . ja v a 2 s. c o 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.BuildingActivity; import com.mintcode.kris.powergridhelper.Models.Player; import com.mintcode.kris.powergridhelper.R; import java.util.ArrayList; import java.util.HashMap; /** * Created by Kris on 8/11/2014. */ public class BuildingAdapter extends ArrayAdapter<Player> { Context mContext; ArrayList<Player> list; private final BuildingActivity callback; HashMap<String, Integer> resourceList; public BuildingAdapter(Context context, int resource, ArrayList<Player> l, HashMap<String, Integer> rl, BuildingActivity cb) { //super(context, l.size()); super(context, resource, l); mContext = context; list = l; callback = cb; resourceList = rl; } @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); } //Show Resource & auction view view.findViewById(R.id.ResourceSummary).setVisibility(View.VISIBLE); view.findViewById(R.id.AuctionLayout).setVisibility(View.VISIBLE); //setup Variables TextView name = (TextView) view.findViewById(R.id.EditPlayerName); TextView money = (TextView) view.findViewById(R.id.AuctionMoney); ImageView color = (ImageView) view.findViewById(R.id.PlayerColor); final Button power = (Button) view.findViewById(R.id.AuctionBuy); 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() + ""); money.setText("$" + list.get(position).getMoney()); name.setText(list.get(position).getName()); color.setBackgroundColor(list.get(position).getColor()); //Change the buy button to power power.setText("Power"); //dialog power.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final MutableInteger cost = new MutableInteger(0); cost.setValue(0); final MutableInteger cities = new MutableInteger(0); cities.setValue(list.get(position).getCities_owned()); LayoutInflater inflater = LayoutInflater.from(mContext); final View dialoglayout = inflater.inflate(R.layout.power_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); Button plusOneCity = (Button) dialoglayout.findViewById(R.id.PlusOneCity); Button minusOneCity = (Button) dialoglayout.findViewById(R.id.MinusOneCity); final TextView totalCities = (TextView) dialoglayout.findViewById(R.id.DialogCityAmount); totalCities.setText("" + cities.getValue()); //Total City Logic minusOneCity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cities.getValue() > list.get(position).getCities_owned()) { cities.setValue(cities.getValue() - 1); totalCities.setText("" + cities.getValue()); } } }); plusOneCity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cities.setValue(cities.getValue() + 1); totalCities.setText("" + cities.getValue()); } }); final TextView total = (TextView) dialoglayout.findViewById(R.id.DialogMoneyAmount); 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 City Space"); //set buttons alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (cost.getValue() <= list.get(position).getMoney()) { dialog.dismiss(); launchNextDialog(position, power, cost,cities); } else { Toast.makeText(mContext, "You can not afford these properties!", Toast.LENGTH_LONG).show(); } } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } }); return view; } private void launchNextDialog(final int position, final Button power, final MutableInteger cost, final MutableInteger cities) { //Show next dialog LayoutInflater inflater = LayoutInflater.from(mContext); final View dialoglayout = inflater.inflate(R.layout.use_resource_dialog, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); alertDialogBuilder.setView(dialoglayout); final 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); 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() < list.get(position).getCoal()) { //Add a coal coalAdd.setValue(coalAdd.getValue() + 1); coalButton.setText("Coal:" + coalAdd.getValue()); } } }); oilButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (oilAdd.getValue() < list.get(position).getOil()) { //Add a oil oilAdd.setValue(oilAdd.getValue() + 1); oilButton.setText("Oil:" + oilAdd.getValue()); } } }); garbageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (garbageAdd.getValue() < list.get(position).getGarbage()) { //Add a coal garbageAdd.setValue(garbageAdd.getValue() + 1); garbageButton.setText("Garbage:" + garbageAdd.getValue()); } } }); uraniumButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (uraniumAdd.getValue() < list.get(position).getUranium()) { //Add a coal uraniumAdd.setValue(uraniumAdd.getValue() + 1); uraniumButton.setText("Uranium:" + uraniumAdd.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); coalButton.setText("Coal:" + coalAdd.getValue()); oilButton.setText("Oil:" + oilAdd.getValue()); garbageButton.setText("Garbage:" + garbageAdd.getValue()); uraniumButton.setText("Uranium:" + uraniumAdd.getValue()); } }); //set title alertDialogBuilder.setTitle("Use Resources"); //set buttons alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialogBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Dialog the THIRD.. launchLastDialog(position, cost,cities, power, coalAdd, oilAdd, garbageAdd, uraniumAdd); dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } //TODO: Clean this dialog mess up private void launchLastDialog(final int position, final MutableInteger city_cost,final MutableInteger cities, final Button power, final MutableInteger coalAdd, final MutableInteger oilAdd, final MutableInteger garbageAdd, final MutableInteger uraniumAdd) { //Show next dialog 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); final MutableInteger poweredNum = new MutableInteger(list.get(position).getCities_powering()); //Resources 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); TextView title = (TextView) dialoglayout.findViewById(R.id.Title); title.setText("Number of Powered Cities:"); final TextView total = (TextView) dialoglayout.findViewById(R.id.DialogMoneyAmount); total.setText("" + list.get(position).getCities_powering()); dialoglayout.findViewById(R.id.DialogPlantID).setVisibility(View.GONE); dialoglayout.findViewById(R.id.idNum).setVisibility(View.GONE); //Calc Logic minusTen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() - 10) >= 0) { poweredNum.setValue(poweredNum.getValue() - 10); total.setText("" + poweredNum.getValue()); } } }); minusFive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() - 5) >= 0) { poweredNum.setValue(poweredNum.getValue() - 5); total.setText("" + poweredNum.getValue()); } } }); minusOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() - 1) >= 0) { poweredNum.setValue(poweredNum.getValue() - 1); total.setText("" + poweredNum.getValue()); } } }); plusTen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() + 10) <= cities.getValue()) { poweredNum.setValue(poweredNum.getValue() + 10); total.setText("" + poweredNum.getValue()); } } }); plusFive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() + 5) <= cities.getValue()) { poweredNum.setValue(poweredNum.getValue() + 5); total.setText("" + poweredNum.getValue()); } } }); plusOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((poweredNum.getValue() + 1) <= cities.getValue()) { poweredNum.setValue(poweredNum.getValue() + 1); total.setText("" + poweredNum.getValue()); } } }); //set title alertDialogBuilder.setTitle("Number of Powered Cities"); //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) { //Stuff for onClick 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()); list.get(position).setMoney(list.get(position).getMoney() - city_cost.getValue()); list.get(position).setCities_powering(poweredNum.getValue()); list.get(position).setCities_owned(cities.getValue()); notifyDataSetChanged(); power.setVisibility(View.GONE); dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } 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; } } }