Search code examples
javaandroidsqlandroid-cursoradapter

Custom CursorAdapter not populating ListView


When I run my activity that has the list view I am trying to populate, it shows as empty. However, in logcat I can see that the custom CursorAdapter runs through my data and returns at least some views.

    public class JobAdapter extends CursorAdapter {

private String status;
private static final String tag ="TESTING CursorAdapter";

public JobAdapter(Context context, Cursor cur, boolean autoRequery, String s) {
    super(context, cur, autoRequery);
    Log.d(tag, "Job adapter create");
    status = s;
}

@Override
public void bindView(View row, Context context, Cursor cursor) {
    Log.d(tag, "Starting a bind");
    Cursor c = getCursor();
    if(!c.isClosed()){  
        TextView companyName = (TextView) row.findViewById(R.id.listCompanyName);
        TextView positionTitle = (TextView) row.findViewById(R.id.listPositionTitle);


        while(!c.isLast() && !c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
            Log.d(tag, "bind - moving c to next 1");
            c.moveToNext();
        }

        if(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
            Log.d(tag, "found a status and trying to populate the view");
            companyName.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));
            positionTitle.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_POSITION)));

            Log.d(tag, "Company name = "+c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));

            if(c.isLast()) {
                Log.d(tag, "bind - Closing c 1");
                c.close();
            }else {
                Log.d(tag, "bind - moving c to next 2");
                c.moveToNext();
            }
         }
    }
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();
    Log.d(tag, "Starting a new");
    final LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(R.layout.job_list_item, parent, false);

    if(!c.isClosed()){  
        TextView companyName = (TextView) row.findViewById(R.id.listCompanyName);
        TextView positionTitle = (TextView) row.findViewById(R.id.listPositionTitle);

        while(!c.isLast() && !c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
            Log.d(tag, "new - moving c to next 1");
            c.moveToNext();
        }

        if(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_STATUS)).equalsIgnoreCase(status)){
            Log.d(tag, "new - found a status and trying to populate the view");
            companyName.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_COMPANY)));
            positionTitle.setText(c.getString(c.getColumnIndex(JobsDbAdapter.KEY_POSITION)));

            if(c.isLast()) {
                Log.d(tag, "new - Closing c 1");
                c.close();
            }else {
                Log.d(tag, "new - moving c to next 2");
                c.moveToNext();
            }

    }
    Log.d(tag, "new - returning row");
    return row;
}

}

Should I be checking to see if the cursor has reached the end of the database ? Or will this be handled for me ?

My class that uses this adapter is simply

    public class AppliedForActivity extends ListActivity {

private JobsDbAdapter mDbHelper;

public void onCreate(Bundle savedInstanceState) {
    Log.d("TESTING C", "STARTING THE APPLIED FOR ACTIVITY");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.job_list);
    mDbHelper = new JobsDbAdapter(this);
    mDbHelper.open();
    Cursor c = mDbHelper.fetchAllJobs();
    setListAdapter(new JobAdapter(this, c, false, "applied for"));
}

}

Please excuse the curly braces, I may have copied wrong.


Solution

  • The idea of CursorAdapter is to incapsulate all the postioning from your code and to leave only visualizing to you. That is you need not use moveToNext and other positioning methods.

    As I can see in your code you are trying to implement some filtering. But the idea of working with database is to formulate your criteria in SQL query. Instead of querying ALL the jobs you should select only those which should be displayed in the ListView.

    bindView and newView should only work with current record the cursor is pointed to and should never call moveToNext or close. Also, CursorAdapter assumes your cursor has an integer field named '_id'.

    See also here and there.