I am in a single class, using 2 different methods.
In one method I have:
private void detect();
int facesFound = detector.findFaces(bitmap565, faces);
detector, bitmap565 and faces are all defined in the same method.
In another method, I would like to call the value of facesFound.
So:
private void crop(){
if (facesFound > 1){
}
My issue is, I cannot access that integer from the method because it is cast locally. What is my best way to alter my code to call it?
Edit: to add method:
private final View.OnClickListener btnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.action_button:
crop();
So are you saying declare an integer at the top of my class that is defined as getting the integer passed back through my new private int detect() method?
Change detect() and crop() to:
private int detect()
{
return detector.findFaces(bitmap565, faces);
}
private void crop(int numberOfFacesFound)
{
if(numberOfFacesFound > 1)
{
}
}
Then, wherever you are calling crop() from:
int numberOfFacesFound = detect();
crop(numberOfFacesFound);
Related
I have two Activities. And a static integer called as counter.
So if I press a button in activity 'A' then counter = counter + 1.
Here is the code from activity a:
public static int counter = 0;
cmdOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter = counter + 1;
if (counter == 5)
{
tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
}
}
And here it is from activity b :
cmdSuccess.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a.counter = a.counter + 1;
if (a.counter == 5)
{
tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
}
}
My problem is when i tried to press a button from activity a 3 times it work perfectly. So the values are 3 now.
But when i tried press a button from activity b, the value is going restart into 0. Actually i didn't destroy activity a.
So what i want is the value is going continouosly even i press from activity a or b.
Any ideas ?
Edited :
I have edit the code. Tagihan activity is what im trying to accomplished. So when the counter is 5, then tagihan activity is changing.
Dont use static data, this is a bad approach and is not a common OOP-way to develope, instead try passing data between activities...
Act1
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
Act2:
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Android development web is giving an introduction to this:
http://developer.android.com/training/basics/firstapp/starting-activity.html
After your edit, I can see that you need a "global varfiable" that can be read/write for all the activities:
Solution:
All activities are embedded in an Application, so if you habe fields/members in the application you can access to them with a stadard setter/getter
you need:
Define an application
public class MyApplication extends Application {
private int counterVariable;
public int counterVariable() {
return this.counterVariable;
}
public void setCounterVariable(int someVariable) {
this.counterVariable = someVariable;
}
}
add the App to the manifest:
<application
android:name="MyApplication"
android:icon="#drawable/icon"
android:label="#string/app_name">
Then in your activities get and set the variable like so:
// cast to Application and call the setter
((MyApplication) this.getApplication()).counterVariable(1);
// cast to Application and call the getter
int counter = ((MyApplication) this.getApplication()).getCounterVariable ();
Please use the below code:
// Generalized form of Avoiding the Static value holding:
public class SPDataHandler {
private Context mContext;
private SharedPreferences mPreference;
public SPDataHandler(Context context) {
this.mContext = context;
this.mPreference = mContext.getSharedPreferences("SAMPLE_SP", Context.MODE_PRIVATE);
}
/**
* COMMON SETTER FOR INTEGER DATA
*/
private void setIntegerData(String key, int value) {
SharedPreferences.Editor editor = mPreference.edit();
editor.putInt(key, value);
editor.commit();
}
/**
* COMMON GETTER FOR INTEGER SP DATA
*/
private int getIntegerSpData(String key, int defaultValue) {
return this.mPreference.getInt(key, defaultValue);
}
// Your Getter and Setter
public int getmCount() {
return this.getIntegerSpData("Count", 1);
}
public void setmCount(int cont) {
this.setIntegerData("Count", cont);
}
}
// Your Activity A
SPDataHandler handler = new SPDataHandler(this);
int count = handler.getmCount();
cmdOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count = count + 1;
handler.setmCount(count); // Change the logic based on your requirement
if (count == 5)
{
tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
}
}
// Your Activity B
SPDataHandler handler = new SPDataHandler(this);
int count = handler.getmCount();
cmdSuccess.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count = count + 1;
handler.setmCount(count); // Change the logic based on your requirement
if (count == 5)
{
tagihan.txtShip1.setTextColor(Color.parseColor("#000000"));
tagihan.txtNilai1.setTextColor(Color.parseColor("#000000"));
tagihan.txtSupir1.setTextColor(Color.parseColor("#000000"));
}
}
I just noticed the Property class http://developer.android.com/reference/android/util/Property.html . I can see some explanation of it here http://developer.android.com/about/versions/android-4.0.html#api but dont really understand the use cases of it. Would be great if someone can point me to some code snippets where I can understand this more.
Property is a wrapper to a Reflection.
For example, you have an object
public class A {
private int fieldOfA;
private int fieldTwo;
private int fieldThree;
public void setFieldOfA(int a) {
fieldOfA = a;
}
public int getFieldOfA() {
return fieldOfA;
}
public void setFieldTwo(int a) {
fieldTwo = a;
}
public int getFieldTwo() {
return fieldTwo;
}
public void setFieldThree(int a) {
fieldThree = a;
}
public int getFieldThree() {
return fieldThree;
}
}
If you need to update phew fields, you have to know all their names in the update method without Properties
private void updateValues(final A a, final int value) {
a.setFieldOfA(value);
a.setFieldTwo(value);
a.setFieldThree(value);
}
With Properties you can update only the properties.
Property aProperty = Property.of(A.class, int.class, "fieldOfA");
Property bProperty = Property.of(A.class, int.class, "fieldTwo");
Property cProperty = Property.of(A.class, int.class, "fieldThree");
Collection<Property<A, Integer>> properties = new HashSet<>();
properties.add(aProperty);
properties.add(bProperty);
properties.add(cProperty);
updateValues(a, 10, properties);
And the method would be
private void updateValues(final A a, final int value, final Collection<Property<A, Integer>> properties) {
for (final Property<A, Integer> property : properties) {
property.set(a, value);
}
}
As laalto memtioned, property animations use a similar mechanism.
One example would be property animations. The Property class provides an abstraction for attributes that can be changed over time to perform an animation.
I have downloaded this code from developer.android
public class SpinnerTestActivity extends
ActivityInstrumentationTestCase2<SpinnerActivity> {
private SpinnerActivity mActivity;
private Spinner mSpinner;
private SpinnerAdapter mPlanetData;
public static final int ADAPTER_COUNT = 9;
public static final int INITIAL_POSITION = 0;
public static final int TEST_POSITION = 5;
private String mSelection;
private int mPos;
public SpinnerTestActivity() {
super("com.android.example.spinner", SpinnerActivity.class);
} // end of SpinnerActivityTest constructor definition
#Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
mActivity = getActivity();
mSpinner = (Spinner) mActivity
.findViewById(com.android.example.spinner.R.id.Spinner01);
mPlanetData = mSpinner.getAdapter();
} // end of setUp() method definition
public void testPreConditions() {
assertTrue(mSpinner.getOnItemSelectedListener() != null);
assertTrue(mPlanetData != null);
assertEquals(mPlanetData.getCount(), ADAPTER_COUNT);
} // end of testPreConditions() method definition
public void testSpinnerUI() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
mSpinner.requestFocus();
mSpinner.setSelection(INITIAL_POSITION);
} // end of run() method definition
} // end of anonymous Runnable object instantiation
); // end of invocation of runOnUiThread
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
for (int i = 1; i <= TEST_POSITION; i++) {
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
} // end of for loop
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
mPos = mSpinner.getSelectedItemPosition();
mSelection = (String) mSpinner.getItemAtPosition(mPos);
TextView resultView = (TextView) mActivity
.findViewById(com.android.example.spinner.R.id.SpinnerResult);
String resultText = (String) resultView.getText();
assertEquals(resultText, mSelection);
}
}
My question is: How the testSpinnerUI is invoked? From where? I have read the junit documentation but cannot figure out.. Thanks!
Stupid question, though. I found the answer in this blog.
The lifecycle of a test case is basically this: construction, setUp(), tests run, tearDown(), and destruction. The setUp() method is used to do any general initialization used by all of specific tests. Each test to be run in the test case is implemented as its own method, where the method name begins with “test”. The tearDown() method is then used to uninitialize any resources acquired by the setUp() method.
Each specific test will have it’s own method beginning with “test” – the “test” method name prefix is case sensitive!
My initial question was how the test method ran, since no one is calling it. But from the above test each method should sart with test, so as to be identified.
Please help me with this, i never ever understanded how things work inside onClickListener, so:
Got my own class:
public class ItemFrame extends LinearLayout{
int item_id;
public ItemFrame(Context context){
super(context);
}
public int getItem_id() {
return item_id;
}
public void setItem_id(int item_id) {
this.item_id = item_id;
}
}
I initalize one and add an onClickListener:
ItemFrame myItemFrame = new ItemFrame(this);
myItemFrame.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//ERROR HERE:
//Cannot refer to a non-final variable myItemFrame inside an inner class
//defined in a different method
//Quick fix: Change modifier of 'myItemFrame' to final.
myItemFrame.setItemId(100);
}
});
So... My question is:
How can i set a data tag of my class, inside the onClickListener???
If i change it to final as Eclipse says, i cant modify it since it is final.
Does it make any sense ?
The OnClickListener that you created is an anonymous inner class and can only refer to final values.
Simply change:
ItemFrame myItemFrame = new ItemFrame(this);
to be:
final ItemFrame myItemFrame = new ItemFrame(this);
You can still modify the ItemFrame object that you created. You just can't reassign a new one to the myItemFrame variable.
There is another way to implement your code, have a look at it.
public class ItemFrame extends LinearLayout implements OnClickListener
{
int item_id;
public ItemFrame(Context context)
{
super(context);
}
public int getItem_id()
{
return item_id;
}
public void setItem_id(int item_id)
{
this.item_id = item_id;
}
public void onClick(View v)
{
setItemId(100);
}
}
The scenario is, I am developing an app which reads integer value from file and store it in integer. And value that I get from that file is always changing cause it is cpu frequency value.
How do I keep watch on this value from my activity ?
The ways in my mind are through broadcast receiver, service, observer etc.
But dont know how do I implement..
You can use a listener:
public class CpuValueReader {
private CpuValueReaderListener listener = null;
int cpuValue;
public void setListener(CpuValueReaderListener listener) {
this.listener = listener;
}
public void startReading() {
// this is an exmaple, you may read the file here
// found value:
setValue(theNewValue);
}
private setValue(int value) {
if (value != cpuValue) {
cpueValue = value;
if (listener != null)
listener.cpuValueChanged(value);
}
}
}
the interface:
public inteface CpuValueReaderListener {
public void cpuValueChanged(int newValue);
}
Using it (just an example):
CpuValueReader instance = new CpuValueReader ();
instance.setListener(new CpuValueReaderListener() {
#Override
public void cpuValueChanged(int newValue) {
// do cool things with new value
}
});
I achieve this using chronometer tick listner. So on every tick of chronometer I read the file and set value to text view
chronometer = (Chronometer)findViewById(R.id.chronometer1);
chronometer.setOnChronometerTickListener(new OnChronometerTickListener()
{
#Override
public void onChronometerTick(Chronometer arg0)
{
//read file here and set the value to my textView
}
});
chronometer.start();