Home > Software engineering >  How to use variables from other Activity for buttons in Custom list view Android studio
How to use variables from other Activity for buttons in Custom list view Android studio

Time:04-30

I am currently building a bare bones game app for university. In the Shop activity of the app I have created a custom list view in order to display items in the shop. Each have a different price etc. so the buttons should all complete different tasks. Following guides I have done this through a switch statement inside my getView() method. However the variables used within these statements are required from the shop activity and I was just wondering if anyone could let me know how to use these variables within my custom adapter. Any help would be great, im rather new to this.

public class CustomBaseAdapter extends BaseAdapter {

    Context context;
    String[] listText;
    int[] listImage;

    LayoutInflater inflater;



    public CustomBaseAdapter(Context ctx, String [] itemList, int [] imageList, Button []
            ButtonList){

        this.context = ctx;
        this.listText = itemList;
        this.listImage = imageList;

        inflater = LayoutInflater.from(ctx);

    }


    @Override
    public int getCount() {
        return listText.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        convertView = inflater.inflate(R.layout.activity_custom_list_view, null);
        TextView txtView = (TextView) convertView.findViewById(R.id.textView);
        ImageView itemImg = (ImageView) convertView.findViewById(R.id.imageIcon);
        Button buttonInd = (Button) convertView.findViewById(R.id.buyButton);
        txtView.setText(listText[position]);
        itemImg.setImageResource(listImage[position]);

        buttonInd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position){
                    case 0:
                        if (firstB==false){
                            if(gold>=1000){
                                firstB = true;
                                gold = gold - 1000;
                                dmgMultiplier = dmgMultiplier *2;
                                setDataShop(dmgMultiplier, gold, LoggedInUser);
                                setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 1:
                        if (secondB==false){
                            if(gold>=3000){
                                secondB = true;
                                gold = gold - 3000;
                                dmgMultiplier = dmgMultiplier *5;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 2:
                        if (thirdB==false){
                            if(gold>=8000){
                                thirdB = true;
                                gold = gold - 8000;
                                dmgMultiplier = dmgMultiplier *10;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 3:
                        if (fourthB==false){
                            if(gold>=12000){
                                fourthB = true;
                                gold = gold - 12000;
                                dmgMultiplier = dmgMultiplier *15;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 4:
                        if (fithB==false){
                            if(gold>=20000){
                                fithB = true;
                                gold = gold - 20000;
                                dmgMultiplier = dmgMultiplier *30;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 5:
                        if (sixB==false){
                            if(gold>=40000){
                                sixB = true;
                                gold = gold - 40000;
                                dmgMultiplier = dmgMultiplier *60;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                        break;
                    case 6:
                        if (sevenB==false){
                            if(gold>=100000){
                                sevenB = true;
                                gold = gold - 100000;
                                dmgMultiplier = dmgMultiplier *150;
                                Shop.setDataShop(dmgMultiplier, gold, LogIn.getUser());
                                Shop.setDataShopBought(firstbBin, secondbBin,
                                        thirdbBin,forthbBin,
                                        fithbBin,sixthbBin,
                                        seventhbBin,LoggedInUser);
                            }
                        }
                }
            }
        });


        return convertView;
    }
}

CodePudding user response:

It it bad practice to do calculation and logic in the adapter.

One way to do it is you can create a click listener interface:

public interface CustomItemClickListener {
     void onClick(int position);
}

Your ShopActivity can implement this interface:

public ShopActivity extends Activity implements CustomItemClickListener {
    
    ...
    private CustomBaseAdapter adapter;
  
    @Override void onCreate(Bundle savedInstanceState){
        adapter = CustomBaseAdapter(this, itemList, imageList, this)
    }

    @Override void onClick(int position){
        //Your switch code block here.
    }
}

And your adapter class:

public class CustomBaseAdapter extends BaseAdapter {

    Context context;
    String[] listText;
    int[] listImage;
    LayoutInflater inflater;
    CustomItemClickListener listener;

    public CustomBaseAdapter(Context ctx, String[] itemList, int[] imageList, CustomItemClickListener listener){

        this.context = ctx;
        this.listText = itemList;
        this.listImage = imageList;

        inflater = LayoutInflater.from(ctx);
        this.listener = listener;

    }
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        buttonInd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });
    }
    ...
}
  • Related