How to pass variable value in another method?

Good day!
I can't figure out how to nextQuoteBtn onClick method to pass the value of pos to copy quotes from the array. Help to implement.
//Button change quote
 NextQuoteBtn Button = (Button) findViewById(R. id.next_quote_btn_quote_activity);
 nextQuoteBtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 final int pos = random.nextInt(quotes.length);
QuoteShowView.setText(quotes[pos]);
}
});

 //Button "copy"
 CopyQuoteBtn ImageButton = (ImageButton) findViewById(R. id.quote_btn_copy);
 copyQuoteBtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 Toast toast = Toast.makeText(getApplicationContext(),
 "Quote copied", Toast.LENGTH_SHORT);
toast.show();

 android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(quotes[pos]);
}
});

 //Button "share"
 ShareQuoteBtn ImageButton = (ImageButton) findViewById(R. id.quote_btn_share);
 shareQuoteBtn.setOnClickListener(new View.OnClickListener() {
 @Overrided 
 public void onClick(View v) {
 Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
 sendIntent.putExtra(Intent.EXTRA_TEXT, quotes[pos]);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent,"Share"));
}
 });
June 5th 19 at 21:44
2 answers
June 5th 19 at 21:46
okay, I understand, but you say the PPC is unclear. You want to use in different anonymous classes of the same variable. Why do you want to I don't know, but it can be done so
final int pos = random.nextInt(quotes.length); is replaced by the assignment field in the containing class. In other places use.
it's easier then to use a non-anonymous classes and inner. Or even a separate, external, so to speak. - dorian53 commented on June 5th 19 at 21:49
,
1) why would it
2) generally easier to do with a normal architecture, but to explain this torment. - Art_Christians commented on June 5th 19 at 21:52
well , in Java, anonymous classes is quite limited - dorian53 commented on June 5th 19 at 21:55
stop your shit - Art_Christians commented on June 5th 19 at 21:58
June 5th 19 at 21:48
I in Java not strong, but if I'm not mistaken there are two options. Either create a global variable which will be assigned the value of pos. Then you can use it in any method. Either you need to change onClick method, replacing the void (returns nothing) to the desired data type, such as String and return the value of pos.
the return value is part of the contract, and so it cannot be changed (or rather, type). But using global variables is bad style. Then you need to work with objects and classes (create the field in the class and return its getter). - dorian53 commented on June 5th 19 at 21:51
Well, you can also return value to write to a new variable and do with it anything? But generally of course you are right, your version is more logical and authentic. - Art_Christians commented on June 5th 19 at 21:54

Find more questions by tags JavaAndroid