Java 如何从第二个活动中获取领域信息?
我从主活动中获得了schoolID,它基于json文件中的realm。使用学校ID,我试图获得学生的名字 我的问题是如何获取学生姓名以及如何从学生适配器中列出学生。谢谢 以下是学生活动:Java 如何从第二个活动中获取领域信息?,java,android,realm,Java,Android,Realm,我从主活动中获得了schoolID,它基于json文件中的realm。使用学校ID,我试图获得学生的名字 我的问题是如何获取学生姓名以及如何从学生适配器中列出学生。谢谢 以下是学生活动: @Override public void onResume() { super.onResume(); Bundle extras = getIntent().getExtras(); if (extras != null) { String schoolName
@Override
public void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String schoolName = extras.getString("schoolName");
RealmResults<School> schools = realm.where(School.class).findAll();
//RealmResults<School> school = realmStudent.where(School.class).equalTo("SchoolName", schoolName).findAll();
// Log.d(tag, String.valueOf(school));
}
}
@Override
public void onResume(){
super.onResume();
if(mAdapter == null) {
List<School> schools = null;
try {
schools = loadSchools();
} catch (IOException e) {
e.printStackTrace();
}
}
RealmResults<School> schools = realm.where(School.class).findAll();
final SchoolAdapter adapter = new SchoolAdapter(this, R.id.schools_list, schools, true);
ListView listView = (ListView) findViewById(R.id.schools_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
String schoolName = adapter.getRealmResults().get(position).getSchoolName();
startStudentList(schoolName);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
public void startStudentList(String schoolName){
//String schoolName = school.getSchoolName();
Intent schoolIntent = new Intent(this, StudentActivity.class);
schoolIntent.putExtra("schoolName", schoolName);
// Log.d(tag, String.valueOf(schoolName));
startActivity(schoolIntent);
}
public List<School> loadSchools() throws IOException{
loadJsonFromStream();
return realm.allObjects(School.class);
}
private void loadJsonFromStream() throws IOException {
InputStream stream = getAssets().open("school.json");
realm.beginTransaction();
try {
realm.createAllFromJson(School.class, stream);
realm.commitTransaction();
} catch (IOException e) {
realm.cancelTransaction();
} finally {
if (stream != null) {
stream.close();
}
}
}
@覆盖
恢复时公开作废(){
super.onResume();
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
String schoolName=extras.getString(“schoolName”);
RealmResults schools=realm.where(School.class).findAll();
//RealmResults school=realmStudent.where(school.class).equalTo(“SchoolName”,SchoolName).findAll();
//Log.d(tag,String.valueOf(school));
}
}
以下是学校班级:
@Required
private String SchoolID;
private String SchoolName;
private RealmList<Student> Students;
@必需
私人字符串学校ID;
私有字符串学名;
私人地产商学生;
主要活动如下:
@Override
public void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String schoolName = extras.getString("schoolName");
RealmResults<School> schools = realm.where(School.class).findAll();
//RealmResults<School> school = realmStudent.where(School.class).equalTo("SchoolName", schoolName).findAll();
// Log.d(tag, String.valueOf(school));
}
}
@Override
public void onResume(){
super.onResume();
if(mAdapter == null) {
List<School> schools = null;
try {
schools = loadSchools();
} catch (IOException e) {
e.printStackTrace();
}
}
RealmResults<School> schools = realm.where(School.class).findAll();
final SchoolAdapter adapter = new SchoolAdapter(this, R.id.schools_list, schools, true);
ListView listView = (ListView) findViewById(R.id.schools_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
String schoolName = adapter.getRealmResults().get(position).getSchoolName();
startStudentList(schoolName);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
public void startStudentList(String schoolName){
//String schoolName = school.getSchoolName();
Intent schoolIntent = new Intent(this, StudentActivity.class);
schoolIntent.putExtra("schoolName", schoolName);
// Log.d(tag, String.valueOf(schoolName));
startActivity(schoolIntent);
}
public List<School> loadSchools() throws IOException{
loadJsonFromStream();
return realm.allObjects(School.class);
}
private void loadJsonFromStream() throws IOException {
InputStream stream = getAssets().open("school.json");
realm.beginTransaction();
try {
realm.createAllFromJson(School.class, stream);
realm.commitTransaction();
} catch (IOException e) {
realm.cancelTransaction();
} finally {
if (stream != null) {
stream.close();
}
}
}
@覆盖
恢复时公开作废(){
super.onResume();
if(mAdapter==null){
列出学校=空;
试一试{
学校=loadSchools();
}捕获(IOE异常){
e、 printStackTrace();
}
}
RealmResults schools=realm.where(School.class).findAll();
最终学校适配器=新学校适配器(此,R.id.schools\u列表,学校,true);
ListView ListView=(ListView)findViewById(R.id.schools\u列表);
setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
公共无效onItemClick(AdapterView AdapterView,视图,
内部位置,长id){
字符串schoolName=adapter.getRealmResults().get(position.getSchoolName();
开始学生名单(学名);
}
});
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
realm.close();
}
public void startStudentList(字符串schoolName){
//字符串schoolName=school.getSchoolName();
意向schoolIntent=新的意向(此,StudentActivity.class);
schoolIntent.putExtra(“学名”,学名);
//Log.d(tag,String.valueOf(schoolName));
星触觉(学校意图);
}
公共列表loadSchools()引发IOException{
loadJsonFromStream();
返回realm.allObjects(School.class);
}
私有void loadJsonFromStream()引发IOException{
InputStream=getAssets().open(“school.json”);
realm.beginTransaction();
试一试{
realm.createAllFromJson(School.class,stream);
realm.commitTransaction();
}捕获(IOE异常){
realm.cancelTransaction();
}最后{
if(流!=null){
stream.close();
}
}
}
以下是学校适配器:
public class SchoolAdapter extends RealmBaseAdapter<School> implements ListAdapter {
private static class ViewHolder{
TextView school;
}
public SchoolAdapter(Context context, int resId, RealmResults<School> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.school = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
School item = realmResults.get(position);
viewHolder.school.setText(item.getSchoolName());
return convertView;
}
public RealmResults<School> getRealmResults() {
return realmResults;
}
}
公共类SchoolAdapter扩展RealmBaseAdapter实现ListAdapter{
私有静态类视图持有者{
文本视图学校;
}
公立学校适配器(上下文上下文、int resId、RealmResults RealmResults、boolean automaticUpdate){
super(上下文、realmResults、automaticUpdate);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
持票人持票人;
if(convertView==null){
convertView=充气机。充气(android.R.layout.simple\u list\u item\u 1,父项,false);
viewHolder=新的viewHolder();
viewHolder.school=(TextView)convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)convertView.getTag();
}
学校项目=realmResults.get(位置);
viewHolder.school.setText(item.getSchoolName());
返回视图;
}
公共RealmResults getRealmResults(){
返回realmResults;
}
}
您可以在XML文件中使用字符串数组。您希望如何处理学生列表。您的学校适配器中已经有学校
。因此,调用item.getStudents()
应该会给出学生列表。然后你可以对他们做任何你想做的事?我想在我点击任何一所学校后获得学生的列表视图。有没有办法在同一活动中做到这一点。当我将学校对象传递给另一个活动时,我无法从学校对象检索该对象,即使我可以将值传递给学生活动。@Spidey您可以将SchoolID
传递给另一个活动,并在那里执行查询以查找学校
对象。然后按照Christian的建议,使用item.getStudents()
作为列表视图。