Android Open Source - PowerGridCompanion Player Setup 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   w  w w. j a  v  a2  s  . c o 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 android.widget.Toast;

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

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

/**
 * Created by kris on 6/29/14.
 */
public class PlayerSetupAdapter extends ArrayAdapter<Player> {

    Context mContext;
    ArrayList<Player> list;


    public PlayerSetupAdapter(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);
        }

        TextView name = (TextView) view.findViewById(R.id.EditPlayerName);
        ImageView color = (ImageView) view.findViewById(R.id.PlayerColor);
        Button edit = (Button) view.findViewById(R.id.EditPlayerSetup);

        edit.setVisibility(View.VISIBLE);

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

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

                //spinner
                ArrayList<String> colors = new ArrayList<String>();
                Collections.addAll(colors, mContext.getResources().getStringArray(R.array.color_array));

                for(Player p : list){
                    if(p.getColor() == Color.RED) {
                        colors.remove(colors.indexOf("Red"));
                    }else if(p.getColor() == Color.BLUE){
                        colors.remove(colors.indexOf("Blue"));
                    }else if(p.getColor() == Color.GREEN){
                        colors.remove(colors.indexOf("Green"));
                    }else if(p.getColor() == Color.YELLOW){
                        colors.remove(colors.indexOf("Yellow"));
                    }else if(p.getColor() == Color.MAGENTA){
                        colors.remove(colors.indexOf("Purple"));
                    }else if(p.getColor() == Color.BLACK){
                        colors.remove(colors.indexOf("Black"));
                    }
                }

                Spinner spinner = (Spinner) dialoglayout.findViewById(R.id.color_spinner);

                // Create an ArrayAdapter using the string array and a default spinner layout
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_dropdown_item,colors);

                spinner.setAdapter(adapter);

                //set title
                alertDialogBuilder.setTitle("Edit Player");


                //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) {

                        list.get(position).setName(((EditText)dialoglayout.findViewById(R.id.editText)).getText().toString());

                        String c = (String)((Spinner) dialoglayout.findViewById(R.id.color_spinner)).getSelectedItem();

                        int color = -1;
                        if(c.equals("Red")){
                            color = Color.RED;
                        }else if(c.equals("Blue")){
                            color = Color.BLUE;
                        }else if(c.equals("Green")){
                            color = Color.GREEN;
                        }else if(c.equals("Yellow")){
                            color = Color.YELLOW;
                        }else if(c.equals("Purple")){
                            color = Color.MAGENTA;
                        }else if(c.equals("Black")){
                            color = Color.BLACK;
                        }

                        if(color != -1) {
                            list.get(position).setColor(color);
                        }

                        notifyDataSetChanged();

                        dialog.dismiss();
                    }
                });

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

                // show it
                alertDialog.show();

                //alertDialog.setOnCancelListener(new DialogCancelListener(getActivity(), alertDialog));


            }
        });

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

        return view;
    }
}




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