如何在androidstudio中返回setonitemclicklistener中的函数?

c9qzyr3d  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(302)

我想为onclicklistener项内的开关内的函数返回一个布尔值。我该怎么做呢?这是我的密码:

public void testForItemClick()
    {
        final Cursor cursor = myDb.getAllData("Main_Values");
        cursor.moveToFirst();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Celebrity celebrity = unpurchasedCelebrities.get(position);
                int money = cursor.getInt(1);
                if (money < celebrity.getPrice())
                    showPurchaseSnackbar("$TooExpensive");
                else
                {
                    switch (celebrity.getCharacteristic()) {
                        case "c":
                            myDb.updateCelebrity(celebrity, 4, true);
                            money = money - celebrity.getPrice();
                            myDb.updateValues("MONEY", money);
                            cursor.moveToNext();

                            int moneyPerClick = cursor.getInt(1);
                            moneyPerClick = moneyPerClick + celebrity.getIncrease();
                            myDb.updateValues("MONEY_PER_CLICK", moneyPerClick);
                            break;

                        case "s":
                            myDb.updateCelebrity(celebrity, 4, true);
                            money = money - celebrity.getPrice();
                            myDb.updateValues("MONEY", money);
                            cursor.moveToNext();

                            cursor.moveToNext();
                            int moneyPerSec = cursor.getInt(1);
                            moneyPerSec = moneyPerSec + celebrity.getIncrease();
                            myDb.updateValues("MONEY_PER_SEC", moneyPerSec);
                            break;

                        case "p":
                            showPrestigeWarning(celebrity);
                            showCelebrities();
                            showPurchaseSnackbar(celebrity.getName());
                            break;
                    }
                }
            }
        });
    }

我这么做只是因为我的开关坏了。如果您有其他解决方案,请转发给我们。

mu0hgdu0

mu0hgdu01#

创建一个本地 boolean 并在 setOnItemClickListener

相关问题