我有主片段,我想传递ArrayList
到Activity
类,我将在ListView中显示结果。
片段类:
public class StudentActivity extends Fragment implements OnClickListener {
}
I have data
ArrayList<> allStudents = new ArrayList();
allStudents.add(new Student("H", 99, 93) );
allStudents.add(new Student("C", 98, 92) );
allStudents.add(new Student("B", 98, 91) );
allStudents.add(new Student("A", 80, 94) );
allStudents.add(new Student("F", 70, 84) );
现在我想发送"allStudents"对象到新的活动类StudentResult();
我使用在片段类:
Intent intent = new Intent(getActivity(), StudentResult.class);
intent.putExtra("ExtraData", allStudents);
startActivity(intent);
和在目标类中显示ListView()中的对象;
public class ResultActivity extends Activity {
public void myData(ArrayList<allStudents> myArray) {
marjslistview = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<allStudents>(this, R.layout.student_list, myArray);
...
ScoreLV.setAdapter(arrayAdapter);
...
}
}
提前感谢!
在Fragment中创建自定义接口:
public interface OnFragmentInteractionListener {
public void onFragmentSetStudents(ArrayList<Student>);
}
在Activity中实现这个接口:
public class YourActivity extends Activity implements YourFragment.OnFragmentInteractionListener {
private ArrayList<Student> allStudents;
}
现在你必须重写声明的方法(在你的Activity
):
@Override
public void onFragmentSetStudents(ArrayList<Student> students) {
allStudents = students;
}
然后你只需要在你的Fragment中启动这个方法:
OnFragmetInteractionListener listener = (OnFragmentInteractionListener) activity;
listener.onFragmentStudents(allStudents)
在你的Activity
:
Bundle bundle = getIntent().getExtras();
ArrayList allStudents = bundle.get("ExtraData");
,我认为你需要定义你的ArrayAdapter
为:
arrayAdapter = new ArrayAdapter<Student>(this, R.layout.student_list, allStudents);
剩下的代码,只需添加上面的代码。