2015-12-15 22 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.