mardi 21 mars 2017

Class variable not changing in IF or Switch statements

Seems simple, but I can't get it to work. I have a public class string variable that should change within a if or switch statement. I haven't declared the variable inside the statement so it should change given the scope, no? But it only reads "FROM" and when I do go to change it in the if statement that applies, it does change to "TO" but only in that instance and reverts back to "FROM". I would pass it along the methods, but the full code is more cluttered and I don't think it's possible to do so.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

String typeOfText = "FROM";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public void onClick(View view) {

    if (view.getId() == R.id.faboptions_favorite) {
        Toast.makeText(MainActivity.this, "FROM", Toast.LENGTH_SHORT).show();
        typeOfText = "FROM";

    } else if (view.getId() == R.id.faboptions_textsms) {
        Toast.makeText(MainActivity.this, "TO", Toast.LENGTH_SHORT).show();
        typeOfText = "TO";

    } else {
        typeOfText = "FROM";
        Toast.makeText(MainActivity.this, "Share", Toast.LENGTH_SHORT).show();

        createDialogSaveInfo();
    }
}


private void readImage(Uri photo) {
    StringBuilder stringBuilder = new StringBuilder();

    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photo);
        Frame imageFrame = new Frame.Builder()
                .setBitmap(bitmap)
                .build();

        final SparseArray<TextBlock> items = textRecognizer.detect(imageFrame);


        for (int i = 0; i < items.size(); i++) {
            TextBlock item = items.valueAt(i);
            stringBuilder.append(item.getValue());
            stringBuilder.append("\n");
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d("VARIABLE","" + typeOfText);

//Will always be FROM 

    if (typeOfText.equals("FROM")) {
        fromAddress = stringBuilder.toString();
    } else if (typeOfText.equals("TO")){
        toAddress = stringBuilder.toString();
    }

}

}

Aucun commentaire:

Enregistrer un commentaire