2015-12-17 37 views
0

我從主要活動中獲得了學校ID,該主要活動基於來自json文件的領域。使用學校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)); 
    } 
} 

以下是學校類:

@Required 
private String SchoolID; 
private String SchoolName; 
private RealmList<Student> Students; 

以下爲主要活動:

@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(); 


     } 
    } 

} 

以下是學校適配器:

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; 
} 

} 
+0

你想要做什麼與學生名單。你的'SchoolAdapter'裏已經有'School'了。所以調用'item.getStudents()'應該給你一個學生名單。那麼你可以隨心所欲地做任何事情嗎? –

+0

點擊任何學校後,我想獲得學生的列表視圖。有沒有辦法在同一個活動中做到這一點。當我將它傳遞給另一個活動時,我無法從學校對象中檢索該對象,即使我可以將該值傳遞給學生活動。 – Spidey

+0

@Spidey您可以將'SchoolID'傳遞給另一個活動,並執行查詢以在其中找到'School'對象。然後,正如基督徒所建議的,在那裏使用'item.getStudents()'作爲你的列表視圖。 – beeender

回答

0

您可以在XML文件中使用字符串數組。

相關問題