【问题标题】:Share List between two functions in two classes in android在android中的两个类中的两个函数之间共享列表
【发布时间】:2016-04-25 14:51:01
【问题描述】:

希望你一切安好

假设我有以下课程:

public class MainActivity extends AppCompatActivity {

public  List<Student> DisplyOnTextView( List< Student > students) {
        return students;


    }
}

功能是接收列表学生。

并假设我还有以下课程:

   public class CustomAdapter extends PagerAdapter {

        public Object instantiateItem(ViewGroup container, int position) {


    }

  }

如何将 List students 从第一堂课的函数返回到第二堂课的函数?

问候。

【问题讨论】:

标签: android constructor parameter-passing


【解决方案1】:

CustomAdapter 类创建一个接受列表作为参数的构造函数,并将传递的值保存为字段。

public class CustomAdapter extends PagerAdapter {

    private List<Student> mData;

    public CustomAdapter(List<Student> students) {
        super();
        mData = students;
    }    

}

【讨论】:

  • 谢谢,但为了清楚起见,我希望在第二类函数中访问第一类函数中的列表,我使用了你的代码,但是当我使用 mDdata 应用程序时说:不幸的是停止了。
  • 确保您没有将null 传递给构造函数。检查您的异常消息,查看问题所在并进行处理。提供的代码只是一个示例,不要指望它在第一次运行时就适合您的应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 2013-02-25
  • 2019-05-31
  • 1970-01-01
相关资源
最近更新 更多