Christian Makilan
Christian Makilan

Reputation: 11

Two Checkboxes Being Checked at the Same Time at the same row

I know this question have already been answered in this link but what if i only want to check the checkbox beside the other checkbox.

just is like this

+-------------------------------+
|column1    | column2 |column3  |
+-------------------------------+
|check here |   ✓     |  ✓     |
+-------------------------------+
|butnothere |    □    |   □     |
+-------------------------------+

I've tried this function

$('#chk1, #chk2').on('click', function(){
    var checked = $(this).is(':checked');
    $('#chk1, #chk2').attr('checked', checked);
});

but it checks all of my checkboxes like this.

+-------------------------------+
|column1    | column2 |column3  |
+-------------------------------+
|check here |   ✓     |   ✓    |
+-------------------------------+
|butnothere |   ✓     |   ✓    |
+-------------------------------+

here is my html table(short copy).

<tr>
    <td width="20">
        <input id="chk1" type="checkbox">
        <input id="chk2" type="checkbox">
    </td>
</tr>

just like the one from above.

Upvotes: 0

Views: 174

Answers (3)

OneCricketeer
OneCricketeer

Reputation: 191728

You have a regular Java class. So, stop using Android methods. Just use regular setters or constructors.

For example,

public class B {
   private int productId; 
   public void setProductId(int id) { productId = id; }
}

Or

public class B {
   private int productId; 
   public B(int productId) { this.productId = productId; }
}

public class A extends Activity {

    ...

    final B b = new B();

    LV_Data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int idProduct = (int) view.getTag();
        Toast.makeText(getApplicationContext(), idProduct + "", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), ((TextView) view.findViewById(R.id.tv_product_name)).getText().toString(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getApplicationContext(), GetDataList.class);

        b.setProductId(idProduct);
        // or B b = new B(idProduct); // but, you can't access this outside of the click listener

        }
    });

}

As soon as you lose this class, you lose the data in b, though

Upvotes: 3

Bek
Bek

Reputation: 8471

use constructor

public class B{
    private int num;
    public B(int num){
       this.num = num;
    }
}

call it

B b = new B(idProduct);

Upvotes: 0

Flutterian
Flutterian

Reputation: 1781

You can use SharedPreferences

Below are the examples to set and get the value from shared preferences.

  1. Set the Value

    SharedPreferences.Editor editor = getSharedPreferences("Use your prefered name", MODE_PRIVATE).edit(); editor.putInt("val", 1); editor.apply();

  2. Get the Value

To use shared preference in non-activity class, follow below steps

  1. Create static variable for context in your main activity.

    public static Context appContext;

  2. In onCreate method initialize the context

    public void onCreate() { appContext= getApplicationContext(); }

  3. Create getter method in same class.

    public static Context getAppContext(){ return appContext; }

  4. In your java class, you need to call below context method.

    Context applicationContext = MyActivity.getAppContext();

  5. Use PreferenceManager Class to get the SharedPreferences .

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); int val = prefs.getInt("val", 0); // where 0 is the default value.

Upvotes: 0

Related Questions