如何将来自其他 Activity 的变量用于自定义列表视图 Android 工作室中的按钮

How to use variables from other Activity for buttons in Custom list view Android studio

我目前正在为大学构建一个简单的游戏应用程序。在应用程序的商店 activity 中,我创建了一个自定义列表视图,以便在商店中显示商品。每个都有不同的价格等,所以按钮应该都完成不同的任务。按照指南,我通过 getView() 方法中的 switch 语句完成了此操作。然而,商店 activity 需要这些语句中使用的变量,我只是想知道是否有人可以让我知道如何在我的自定义适配器中使用这些变量。任何帮助都会很棒,我对此很陌生。

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;
    }
}

在适配器中进行计算和逻辑是不好的做法。

一种方法是创建点击监听器界面:

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

您的 ShopActivity 可以实现这个接口:

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.
    }
}

还有你的适配器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);
            }
        });
    }
    ...
}