Android save Checkbox State in ListView with Cursor Adapter

后端 未结 5 1937
忘掉有多难
忘掉有多难 2020-11-27 19:48

I cant find a way to save the checkbox state when using a Cursor adapter. Everything else works fine but if i click on a checkbox it is repeated when it is recycled. Ive see

5条回答
  •  眼角桃花
    2020-11-27 20:31

    3. Code Code
    Attach listeners inside your activity “onCreate()” method, to monitor following events :
    
    If checkbox id : “chkIos” is checked, display a floating box with message “Bro, try Android”.
    If button is is clicked, display a floating box and display the checkbox states.
    File : MyAndroidAppActivity.java
    
    package com.mkyong.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.Toast;
    
    public class MyAndroidAppActivity extends Activity {
    
      private CheckBox chkIos, chkAndroid, chkWindows;
      private Button btnDisplay;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        addListenerOnChkIos();
        addListenerOnButton();
      }
    
      public void addListenerOnChkIos() {
    
        chkIos = (CheckBox) findViewById(R.id.chkIos);
    
        chkIos.setOnClickListener(new OnClickListener() {
    
          @Override
          public void onClick(View v) {
                    //is chkIos checked?
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(MyAndroidAppActivity.this,
                   "Bro, try Android :)", Toast.LENGTH_LONG).show();
            }
    
          }
        });
    
      }
    
      public void addListenerOnButton() {
    
        chkIos = (CheckBox) findViewById(R.id.chkIos);
        chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
        chkWindows = (CheckBox) findViewById(R.id.chkWindows);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
    
        btnDisplay.setOnClickListener(new OnClickListener() {
    
              //Run when button is clicked
          @Override
          public void onClick(View v) {
    
            StringBuffer result = new StringBuffer();
            result.append("IPhone check : ").append(chkIos.isChecked());
            result.append("\nAndroid check : ").append(chkAndroid.isChecked());
            result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
    
            Toast.makeText(MyAndroidAppActivity.this, result.toString(),
                    Toast.LENGTH_LONG).show();
    
          }
        });
    
      }
    }
    

提交回复
热议问题