所有Task.java
jmeter.save.saveservice.assertion_results_failure_message=true
AllReports.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"));
}
}
我想在第二个活动的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);
}
}
中的第一个活动中显示ArrayList中的数据,但是当我试图从第一个活动中获取第二个活动的数据时,它给ListView
表示ArrayList为空。如何在第二个活动中获取ArrayList的内容。
答案 0 :(得分:2)
您可以设置companyList
静态
static ArrayList<Company> companyList;
并将其称为:
AllTask.companyList
不建议使用此方法。您应该在Serializable
类中实现Parcelable
或Company
,并传递如下数据:
Bundle bundle = new Bundle();
bundle.putSerializable("data", companyList);
intent.putExtras(bundle);
并从第二个活动中读取它:
Bundle bundle = getIntent.getExtras();
List<Company> data= (List<Company>)bundle.getSerializable("data");
答案 1 :(得分:1)
首先,将您的Company对象设为Serializable
public class Company implements Serializable {
//Your code
}
从AllTask Activity中你应该像这样打开AllReports,
Intent intent = new Intent(this, AllReports.class);
intent.putExtra("companyList", companyList);
startActivity(intent);
你可以像这样进入AllReports,
ArrayList<Company> companyList = (ArrayList<Company>)getIntent().getSerializableExtra("companyList");
答案 2 :(得分:0)
你应该通过意图发送它。
首先让你的Company bean实现Serializable
然后在你的firstActivity中执行此操作:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("array",companyList);
startActivity(intent);
然后在你onCreate的SecondActitvity中这样做:
ArrayList<Company> companyList = (ArrayList<Company>) getIntent().getSerializableExtra("array");
答案 3 :(得分:0)
您可以将Parcelable数组传递给您的活动。至少对于Android来说,它比Serializable更有效。但是贵公司应该为此实施Parcelable。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, SecondActivity.class);
ArrayList<Company> companies = new ArrayList<>();
//fill companies
i.putParcelableArrayListExtra("EXTRA", companies);
startActivity(i);
}
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Company> companies = getIntent().getParcelableArrayListExtra("EXTRA");
}
}
答案 4 :(得分:0)
我将使用非原生解决方案回答。
您还可以使用绿色机器人EventBus来实现您的目标,而无需使用Serializable
或Parcelable
的样板。当您处理大型数据集或许多活动调用时,其优势明显快于两者。如果你的pojo / bean / class有很多字段,它还有一个不必写太多样板代码的优点。
首先,在build.gradle
添加依赖项。
compile 'org.greenrobot:eventbus:3.0.0'
然后,定义一个Event类,负责通过应用程序传输数据。请注意,这可能是从Activity
到另一个Fragment
。从Service
到另一个。 Activity
返回public class MessageEvent {
public ArrayList<Company> mCompanyList;
// Add additional fields here if needed
public MessageEvent(ArrayList<Company> list) {
this.mCompanyList = list;
}
}
,依此类推。
<强> MessageEvent.java 强>
stickyEvent
现在,您可以定义订阅者,也可以发布ActivityB
并在public class ActivityB extends AppCompatActivity {
// ...
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
EventBus.getDefault().removeStickyEvent(event);
ArrayList<Company> list = event.mCompanyList;
// Do whatever you want to do with the data.
};
}
上获取。就像下面的两个例子一样。
<强> ActivityB.java 强>
public class ActivityA extends AppCompatActivity {
// ...
private void startActB(ArrayList<Company> list) {
Intent it = new Intent(ActivityA.this, ActivityB.class);
EventBus.getDefault().postSticky(new MessageEvent(list));
startActivity(it);
}
}
<强> ActivityA.java 强>
ActivityB
MessageEvent
上发送粘性事件而不订阅<强> ActivityA.java 强>
就像上面的示例一样,发布ActivityB
并开始public class ActivityB extends AppCompatActivity {
private ArrayList<Company> mCompanyList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// Other stuff you might want to do here.
// Get the event that was posted on Activity A.
MessageEvent event = EventBus.getDefault().removeStickyEvent(MessageEvent.class);
// Check if it exists.
if (event != null) {
mCompanyList = event.mCompanyList;
// Do whatever you want with it.
}
else {
// Do something if the event wasn't posted prior to this Activity call.
}
}
// ...
}
。
<强> ActivityB.java 强>
Serializable
与Parcelable
和{{1}}相比,您可以参考this link来查看EventBus的运行速度。
有关详细信息,您可以查看project page或this tutorial。
答案 5 :(得分:0)
使用Application类执行此操作的好方法是,它允许您在各种Activity,Fragment或Dialog中使用相同的对象。
只创建一个对象,可以多次使用 从应用程序的任何位置访问 我正在添加一个我使用的示例代码:
public class Application extends Application {
ArrayList<Filepath> arrayFilePath;
@Override
public void onCreate() {
super.onCreate();
this.sharedPreferences = getSharedPreferences(Constant.Userdata.USER_PREF.name(), Context.MODE_PRIVATE);
}
public ArrayList<Filepath> getArrayListForImagePath() {
String fileurl = sharedPreferences.getString(Constant.Userdata.ARRAYLISTFILEPATH.name(), "");
Gson gson = new Gson();
if (fileurl.isEmpty()) {
arrayFilePath = new ArrayList<Filepath>();
} else {
Type type = new TypeToken<ArrayList<Filepath>>() {
}.getType();
arrayFilePath = gson.fromJson(fileurl, type);
}
return arrayFilePath;
}
public void setArrayListForImagePath(ArrayList<Filepath> imagePath) {
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String jsonFilepath = gson.toJson(imagePath);
editor.putString(Constant.Userdata.ARRAYLISTFILEPATH.name(), jsonFilepath);
editor.commit();
}
public void clearArrayListForImagePath() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(Constant.Userdata.ARRAYLISTFILEPATH.name()).commit();
}
常数类:
public class Constant {
enum Userdata {
USER_PREF, USER_ID, ARRAYLISTFILEPATH
}
}