Venkata Venkata - 1 month ago 10
Android Question

Not able to display ListView using custom Adapter

I am unable to display the data onto my activity which I am reading from SQLite database and using Custom Adapter. I am new to Android. Please correct me, if I have done anything wrong. Here is my code:

DB Code:

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


Custom Adapter Code:

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


Course Display Code:

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


Main Activity code:

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


This is my code. Please correct me where I have gone wrong.

Answer

I think the problem is from your database method. You need two values for your List but you are get only 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

I have rewrote your method and the List take an Object of CourseDisplay instead of String in your own case.

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

Also, the CourseDisplay class will look like this if you don't have it already

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

Finally, inside the getView() method you can change these lines

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

to

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