我正在尝试使用listfragment来显示20个项目。因为它只显示列表中所有二十个项目的列表中的最后一项。我一直在看listfragments的例子,我不知道出了什么问题。
这是来自扩展ListFragment的CourseFragment类:
private List<Course> mCourses;
public CourseFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCourses = Schedule.get(getActivity()).getSchedule();
CourseArrayAdapter<Course> mAdapter = new CourseArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mCourses);
setListAdapter(mAdapter);
for (int i = 0; i < 20; i++ ) {
Log.d("DEBUGTAG", "Course: " + mCourses.get(i));
}
}
这是来自CourseArrayAdapter:
public CourseArrayAdapter(Context context, int resource, List<T> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("DEBUG", "getView " + position);
LayoutInflater inflater = LayoutInflater.from(getContext());
View mLayout;
if (convertView == null) {
mLayout = inflater.inflate(R.layout.view_course, parent, false);
} else {
mLayout = convertView;
}
T mCourse = getItem(position);
TextView mCourseTextView = (TextView) mLayout.findViewById(R.id.course_textview);
mCourseTextView.setText(mCourse.toString());
return mLayout;
}
MainActivity的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager mFragManager = getFragmentManager();
Fragment mFragment = mFragManager.findFragmentById(R.id.main_activity_framelayout);
if(mFragment == null) {
mFragment = new CourseFragment();
mFragManager.beginTransaction().add(R.id.main_activity_framelayout, mFragment).commit();
}
}
以下是Course类的实例变量和构造函数:
private static String sCourseName;
private static int sCourseNum;
private static String sInstructor;
private static String sDescription;
public Course(String mCourseName, int mCourseNum, String mInstructor, String mDescription) {
sCourseName = mCourseName;
sCourseNum = mCourseNum;
sInstructor = mInstructor;
sDescription = mDescription;
}
如果需要任何其他代码来解决此问题,请告知我们。非常感谢你的帮助!
答案 0 :(得分:0)
现在请试试这个mCourses会有数据。我认为不会重复
private List<Course> mCourses;
public CourseArrayAdapter(Context context, int resource, List<T> objects) {
super(context, resource, objects);
mCourses=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("DEBUG", "getView " + position);
LayoutInflater inflater = LayoutInflater.from(getContext());
View mLayout;
if (convertView == null) {
mLayout = inflater.inflate(R.layout.view_course, parent, false);
} else {
mLayout = convertView;
}
// T mCourse = getItem(position);
TextView mCourseTextView = (TextView) mLayout.findViewById(R.id.course_textview);
mCourseTextView.setText(mCourses.get(position).toString());
return mLayout;
}