在另一个类

时间:2016-06-28 04:42:19

标签: java android

我的活动中有一个微调器包含Opeartor列表我希望将该列表放在其他类中并在微调器适配器中调用它的引用。我可以这样做吗

这是运营商列表

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");

}

} 这是COperatorStorage: -

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

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

}

这是我活动中的微调器

ArrayAdapter<COperatorStorage> adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, item);
    m_OperatorSpinner.setAdapter(adapter);
    m_OperatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        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);
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

这里的项目是我想在微调器适配器中调用的引用。

3 个答案:

答案 0 :(得分:1)

按照以下步骤操作:

首先声明一个公共类,您可以根据自己的需要提供任何名称

我将其命名为“CommonClass.java”

<强> CommonClass.java

  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") };
   }

现在,随时随地使用此处,

<强> MainActivity.java

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

    m_OperatorSpinner.setAdapter(adapter);

答案 1 :(得分:1)

你可以在任何一个类中制作一个arraylist public static,如下所示:

public class CommonUtils{
     public static ArrayList<COperatorStorage> listItems=new ArrayList<COperatorStorage>();

}

将数据添加到此列表中,无论您喜欢哪个类别,如下所示:

 .........
 .........
 CommonUtils.listItems.add(new COperatorStorage("Select Operator", null));
 CommonUtils.listItems.add(new COperatorStorage("Aircel", "AIRCEL"));
 CommonUtils.listItems.add(new COperatorStorage("BSNL", "BSNL"));
 .........
 .........

您可以在要显示spinnerand的类中使适配器填充全局arraylist,如下所示:

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

m_OperatorSpinner.setAdapter(adapter);

答案 2 :(得分:0)

只需创建COperatorData的实例,并可能将方法public void operator(){更改为构造函数public COperatorData(){

然后实现public COperatorStorage[] getItems()方法,并将其用于适配器。

COperatorData data = new COperatorData();
adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, data.getItems());