无法使用自定义适配器

时间:2016-10-12 23:33:36

标签: android

我无法将数据显示在我从SQLite数据库和使用自定义适配器读取的活动中。我是Android新手。如果我做错了什么,请纠正我。这是我的代码:

数据库代码:

public List getAllCoursesForRegistration() {

    List courses = new ArrayList<>();

    openDataBase();

    Cursor mCursor = database.query(TABLE_COURSES, new String[] {      COLUMN_COURSE_CODE,COLUMN_COURSE_NAME },null,null,
            null, null, null);
    mCursor.moveToFirst();

    while(!mCursor.isAfterLast()){
        courses.add(mCursor.getString(0));
        mCursor.moveToNext();
    }
    mCursor.close();
    close();//close Database
    return courses;
}//end

自定义适配器代码:

    public class CourseSelectionAdapter extends ArrayAdapter{


    private Context mContext;
    private LayoutInflater mInflater;
    int layoutResourceId;
    List<CourseDisplay> mDataSource;


    public CourseSelectionAdapter(Context context, int layoutResourceId, List<CourseDisplay> items) {
        super(context,layoutResourceId,items);

        mContext = context;
        mDataSource = items;
        this.layoutResourceId = layoutResourceId;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public Object getItem(int position)
    {
        return mDataSource.get(position);
    }


    //4
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get view for row item
        View row = convertView;
        ViewHolder holder = null;

// 1
        if(convertView == null) {

            // 2
            row = mInflater.inflate(R.layout.custom_listview_layout, parent, false);

            // 3
            holder = new ViewHolder();
            holder.courseCodeTextView = (TextView) row.findViewById(R.id.course_id_display);
            holder.courseNameTextView = (TextView) row.findViewById(R.id.course_name_display);

            // 4
            row.setTag(holder);
        }
        else{
            // 5
            holder = (ViewHolder) row.getTag();
        }

// 6
        TextView courseCodeTextView = holder.courseCodeTextView;
        TextView courseNameTextView = holder.courseNameTextView;

        CourseDisplay courseDisplay = (CourseDisplay) getItem(position);
        holder.courseCodeTextView.setText(courseDisplay.courseCode);
        holder.courseNameTextView.setText(courseDisplay.courseName);

        return convertView;
    }
    private static class ViewHolder {
        public TextView courseCodeTextView;
        public TextView courseNameTextView;
    }

}//end class

课程展示代码:

    public class CourseDisplay {
    public String courseCode;
    public String courseName;
    public CourseDisplay(){
        super();
    }

    public CourseDisplay(String courseCode, String courseName) {
        super();
        this.courseCode = courseCode;
        this.courseName = courseName;
    }
}

主要活动代码:

DatabaseOpenHelper myDbHelper = new DatabaseOpenHelper(this);//New Object of Database

    List<CourseDisplay> courseDisplay_data = new ArrayList<CourseDisplay>();

    courseDisplay_data = myDbHelper.getAllCoursesForRegistration();


    CourseSelectionAdapter newAdapter = new CourseSelectionAdapter(this,R.layout.custom_listview_layout,courseDisplay_data);

    myListView = (ListView)findViewById(R.id.listview_courses);
    myListView.setChoiceMode(2);
    myListView.setAdapter(newAdapter);

这是我的代码。请纠正我出错的地方。

1 个答案:

答案 0 :(得分:0)

我认为问题来自您的数据库方法。您的列表需要两个值,但只能获得courseCode。

public List getAllCoursesForRegistration() {
    List courses = new ArrayList<>();
    openDataBase();
    Cursor mCursor = database.query(TABLE_COURSES, new String[] {      COLUMN_COURSE_CODE,COLUMN_COURSE_NAME },null,null,
        null, null, null);
    mCursor.moveToFirst();

    while(!mCursor.isAfterLast()){
        courses.add(mCursor.getString(0));
        mCursor.moveToNext();
    }
    mCursor.close();
    close();//close Database
    return courses;
}//end

我已经重写了你的方法,并且List在你自己的情况下采用了CourseDisplay的Object而不是String。

public List<CourseDisplay> getAllCoursesForRegistration() {
    List<CourseDisplay> courses = new ArrayList<>();
    openDataBase();
    Cursor mCursor = database.query(TABLE_COURSES, new String[] {COLUMN_COURSE_CODE,COLUMN_COURSE_NAME }, null, null, null, null, null);
    if(mCursor.moveToFirst()){
        do{
            String courseCode = mCursor.getString(0);
            String courseName = mCursor.getString(1);
            courses.add(new CourseDisplay(courseCode, courseName));
        }while(mCursor.moveToNext());
    }
    mCursor.close();
    close();//close Database
    return courses;
}

此外,如果你还没有,CourseDisplay课程将会是这样的

public class CourseDisplay{
    private String courseCode;
    private String courseName;

    public CourseDisplay(String courseCode, String courseName) {
        this.courseCode = courseCode;
        this.courseName = courseName;
    }

    public String getCourseCode() {
        return courseCode;
    }

    public String getCourseName() {
        return courseName;
    }
}

最后,在getView()方法中,您可以更改这些行

 holder.courseCodeTextView.setText(courseDisplay.courseCode);
 holder.courseNameTextView.setText(courseDisplay.courseName);

 holder.courseCodeTextView.setText(courseDisplay.getCourseCode());
 holder.courseNameTextView.setText(courseDisplay.getCourseName());