I have 2 classes BuyCoins and CustomOnItemSelectListerner. The BuyCoins class contains a spinner and once the spinner is selected the CustomOnItemSelectListerner class is called.
public class BuyCoins extends AppCompatActivity {
...
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
//TextView t=(TextView) findViewById(R.id.conversion);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener(t));
}
...
}
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
//private TextView t;
//CustomerOnItemSelected (TextView V) {t=V;}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected= parent.getItemAtPosition(pos).toString();
TextView t=(TextView)parent.findViewById(R.id.conversion);
switch(selected) {
case "A":
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + selected,
Toast.LENGTH_SHORT).show();
t.setText("$ala$");
break;
}
...
}
My issue is that t.setText returns null, however the rest of the case statement works fine. Please help.
Layout file with the spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:paddingRight="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Purchase Coins"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/coinpic"
android:src="@drawable/coin"
android:scaleX="0.8"
android:scaleY="0.8"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/coin_array"
android:prompt="@string/coin_prompt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/conversion"
android:layout_gravity="right" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purchase Coins"
android:id="@+id/Pur_button"
android:layout_gravity="center_horizontal"
android:onClick="toast"/>
</LinearLayout>