Reputation: 11
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
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
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
Reputation: 1781
You can use SharedPreferences
Below are the examples to set and get the value from shared preferences.
Set the Value
SharedPreferences.Editor editor = getSharedPreferences("Use your prefered name", MODE_PRIVATE).edit();
editor.putInt("val", 1);
editor.apply();
Get the Value
To use shared preference in non-activity class, follow below steps
Create static variable for context in your main activity.
public static Context appContext;
In onCreate method initialize the context
public void onCreate() {
appContext= getApplicationContext();
}
Create getter method in same class.
public static Context getAppContext(){
return appContext;
}
In your java class, you need to call below context method.
Context applicationContext = MyActivity.getAppContext();
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