所有任务.java
public class AllTask extends AppCompatActivity{
ArrayList<Company> companyList;
Bundle extras;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_list);
companyList=new ArrayList<>();
companyList.add(new Company("Kony Labs","10:30","Good"));
companyList.add(new Company("Delloite","12:30","Very Good"));
companyList.add(new Company("Accenture","14:30","Average"));
companyList.add(new Company("Microsoft","16:30","Very Good"));
companyList.add(new Company("TCS","18:30","Good"));
}
}
AllReports.java
public class AllReports extends AppCompatActivity {
ArrayList<Company> report_companyList;
Bundle extras;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_reports);
getSupportActionBar().setTitle("Reports");
AllTask all_tasks=new AllTask();
report_companyList=new ArrayList<>(all_tasks.companyList);
ListView listView = (ListView) findViewById(R.id.report_list);
MyAdapterResults myAdapter=new MyAdapterResults(this,R.layout.list_view_row_item,report_companyList);
listView.setAdapter(myAdapter);
}
}
我想在第二个 Activity 的 ListView
中的第一个 Activity 中的 ArrayList 中显示数据,但是当我试图从第二个 Activity 中的第一个 Activity 中获取数据时,它给出了 NullPointerException
ArrayList 是空的。如何在第二个activity中获取ArrayList的内容。
最佳答案
你可以让你的companyList
静态
static ArrayList<Company> companyList;
然后这样调用它:
AllTask.companyList
不推荐这种方法。您应该改为在您的 Company
类中实现 Serializable
或 Parcelable
并像这样传递数据:
Bundle bundle = new Bundle();
bundle.putSerializable("data", companyList);
intent.putExtras(bundle);
然后像这样从第二个 Activity 中读取它:
Bundle bundle = getIntent.getExtras();
List<Company> data= (List<Company>)bundle.getSerializable("data");
关于android - 如何从第二个 Activity 中的第一个 Activity 中获取数组列表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45548223/