Siddharth Siddharth - 3 months ago 7
Android Question

Use refrence of variable in another class

I have a spinner in my activity that contain list of Opeartor I want to put that list in other class and call its reference in spinner adapter .how can I do that

here is the list of Operator

public class COperatorData {
protected COperatorStorage item[];

public void operator(){
item = new COperatorStorage[5];
item[0] = new COperatorStorage("Select Operator", null);
item[1] = new COperatorStorage("Aircel", "AIRCEL");
item[2] = new COperatorStorage("BSNL", "BSNL");
item[3] = new COperatorStorage("Idea", "IDEAS");
item[4] = new COperatorStorage("Vodafone", "VODAS");


here is COperatorStorage:-

public class COperatorStorage {
public String name;
public String code;

public COperatorStorage(String name ,String code){ = name;
this.code = code;
public String getName(){
return name;
public String getCode(){
return code;
public String toString(){
return name;


and here is spinner in my activity

ArrayAdapter<COperatorStorage> adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, item);
m_OperatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
COperatorStorage operatorName = item[position];
String selectedOperatorCode = operatorName.code;
Log.e(TAG, "Operator code:-" + selectedOperatorCode);
public void onNothingSelected(AdapterView<?> parent) {


here item is the reference which I want to call in spinner adapter.


Follow below steps :

First Declare one common class, you can give any name as per your suitable

I am giving it name as ""

  public class CommonClass
      public static final COperatorStorage item[] = {new COperatorStorage("Select Operator", null), new COperatorStorage("Aircel", "AIRCEL"), new COperatorStorage("BSNL", "BSNL"), new COperatorStorage("Idea", "IDEAS"), new COperatorStorage("Vodafone", "VODAS") };

Now, use this anywhere like below whenever you want,

    ArrayAdapter<COperatorStorage> adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, CommonClass.item);