2015-12-15 26 views
1

我可以使用適配器在我的主要活動中打印學校名稱,如下面的代碼。如何在getView中列出活動中的領域列表?

我的問題是,如果我點擊學校名稱並想在另一個學生活動中列出學生姓名,我如何列出RealmList<Student> Students的學生姓名,以及如何指定學生在學生適配器中的位置?

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

public class School extends RealmObject { 

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

    //...getters/setters 
} 


public class Student extends RealmObject { 

    @Required 
    private String StudentID; 
    private String StudentName; 

    //...getters/setters 
} 

回答

1

1 - 你應該實現一個OnItemClick監聽你的學校列表視圖:

listViewSchool.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) 
    { 
     // Get the School Name from School Adapter 

     String schoolName = ((School) adapterView.getItemAtPosition(position)).[GetSchoolName()]; 

     // Create intent to pass data and call another Activity. 

     Intent intent = new Intent(activity, [YourStudentActivity].class); 

     // Add data to intent 

     intent.putExtra("schoolName", schoolName); 

     // Call the Student Activity 

     startActivity(intent); 
    } 
}); 

2 - 你應該過濾的學生通過學校名稱在OnCreate()創建學生活動時,向他們展示在學生列表視圖中。爲了得到價值從意圖來:

// OnCreate on Student Activity 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    ... 
    Bundle extras = getIntent().getExtras(); 

    if (extras != null) 
    { 
     String schoolName = extras.getString("schoolName"); 
    } 

    // Then filter the students by [schoolName] and then add the result to the student array used by the student adapter and then notify the student listview. 

    ... 
} 

也許它可以幫助:Getting wrong result in Android application from PHP Server

問:爲什麼你需要指定在學生適配器學生立場?

1
you have to implement the onclick for school row and o click of that have to call another activity with putting school id as extra. 

then on student you have to fetch students list on the basis of school id you send from first list. and the generate the list here.