在 setOnCheckedChangeListener 方法中执行 Toast 消息

Execute a Toast message inside a setOnCheckedChangeListener method

我有一组切换按钮。游标中的行数与 toggleButtons 的比例为 1:1。我将一个 setId() 分配给数组中的每个 toggleButton,其中包含从游标返回的数据。

我的更新总是更新光标中向上的 1 行,我正在尝试使用一些 toast 消息来解决此问题。问题是我无法显示 toast 消息。我知道方法是监听器正在触发,因为我的 sql 更新是 运行,只是它是错误的行。这是我的代码。

toggleButton[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            ContentValues values = new ContentValues(); //Create the new content values object for data input
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if(isChecked)
                {
                    Toast.makeText(Cart.this.getBaseContext(), "hello", Toast.LENGTH_SHORT).show();
                    toggleButton[buttonView.getId()].setTextOn("Yes"); //Set the text when the button changes state to on
                    values.put(Contract.ItemsTable.COLUMN_ONE, 1); //Update the record on the 'status' column to true
                    String[] itemId = {String.valueOf(buttonView.getId())}; //
                    db.update(Contract.ItemsTable.TABLE_NAME, values, SmartCartContract.ItemsTable.COLUMN_ITEM_ID + " LIKE ?", itemId); //Update the DB record
                } else
                {
                    Toast.makeText(Cart.this.getBaseContext(), "hello", Toast.LENGTH_SHORT).show();
                    toggleButton[buttonView.getId()].setTextOff("No"); //Set the text when the button changes state to off
                    values.put(SmartCartContract.ItemsTable.COLUMN_TWO, 0); //Update the record on the 'status' column to false
                    String[] itemId = {String.valueOf(buttonView.getId())}; //
                    db.update(Contract.ItemsTable.TABLE_NAME, values, Contract.ItemsTable.COLUMN_ITEM_ID + " LIKE ?", itemId); //Update the DB record
                }
            }
        });

Cart.this.getBaseContext() 是我最后一次尝试使用我见过的选项。这是我做错了吗?

我正在动态构建这些按钮:

toggleButton[i] = new ToggleButton(this); //Create new ToggleButton control for this row    
toggleButton[i].setGravity(Gravity.CENTER); //Align the text in the middle 
toggleButton[i].setId(cursor.getInt(cursor.getColumnIndex("itemId"))); 
if (cursor.getInt(cursor.getColumnIndex("itemInCart")) == 1) //Check if marked 'in Cart'

不确定 Cart 是什么类型的对象。但是您可以扩展应用程序 class 并保存应用程序上下文,然后您可以在 toasts 中使用它。

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class BaseApplication extends Application
{
    protected static Context context;
    protected static PackageInfo packageInfo;
    protected static BaseApplication baseApplication;

    @Override
    public void onCreate()
    {
        try
            {

                baseApplication = this;

                context = getApplicationContext();

                PackageManager manager = context.getPackageManager();
                String packageName = context.getPackageName();

                packageInfo = manager.getPackageInfo(packageName, 0);
            }
        catch (Exception e)
            {
                // ignore
            }
    }

    public static Context getContext()
    {
        return context;
    }

    public static PackageInfo getPackageInfo()
    {
        return packageInfo;
    }

    public static BaseApplication getApplication(){
        return baseApplication;
    }
}

不确定数据库的问题是什么,但这里是一些 ToggleButtons 工作 Toast 的最小示例。

public class CartActivity extends AppCompatActivity {

    private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {

        ContentValues cv;

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            cv = new ContentValues();
            String key;
            int val;

            if (isChecked) {
                key = "col1";
                val = 1;
            } else {
                key = "col2";
                val = 0;
            }

            cv.put(key, val);
            String[] itemId = {String.valueOf(buttonView.getId())};
            Toast.makeText(CartActivity.this,
                    String.format("[%d] %s {%s}", buttonView.getId(), isChecked, cv),
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout view = new LinearLayout(this);
        view.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        ToggleButton[] buttons = new ToggleButton[3];
        for (int i = 0; i < buttons.length; i++) {
            ToggleButton toggle = new ToggleButton(this);

            toggle.setLayoutParams(lp);
            toggle.setId(i);
            toggle.setGravity(Gravity.CENTER);            
            toggle.setTextOn(String.format("On [%d]", i));
            toggle.setTextOff(String.format("Off [%d]", i));
            toggle.setChecked(false);
            toggle.setOnCheckedChangeListener(listener);

            buttons[i] = toggle;
            view.addView(toggle);
        }

        setContentView(view);
    }
}