2011-10-29 5 views
0

채우려는 목록보기가있는 활동을 실행할 때 비어있는 것으로 표시됩니다. 그러나 logcat에서 사용자 지정 CursorAdapter가 내 데이터를 통해 실행되고 적어도 일부보기를 반환하는 것을 볼 수 있습니다.ListView를 채우지 않은 사용자 지정 CursorAdapter

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

}

나는 커서가 데이터베이스의 끝에 도달 있는지 확인해야 하는가? 아니면 나를 위해 처리 될 것인가? 이 어댑터를 사용

내 클래스는 단순히

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

}

는 중괄호를 용서하십시오, 내가 잘못 복사 한 수 있습니다.

답변

1

CursorAdapter의 아이디어는 코드에서 모든 위치 정보를 캡슐화하고 시각화 만 남기는 것입니다. 즉, moveToNext와 다른 위치 지정 메소드를 사용할 필요가 없습니다.

코드에서 볼 수 있듯이 일부 필터링을 구현하려고합니다. 그러나 데이터베이스로 작업한다는 생각은 SQL 쿼리에 기준을 공식화하는 것입니다. 모든 작업을 쿼리하는 대신 ListView에 표시되어야하는 작업 만 선택해야합니다.

bindView 및 newView는 커서가 가리키는 현재 레코드에서만 작동해야하며 moveToNext 또는 close를 호출하면 안됩니다. 또한 CursorAdapter는 커서에 '_id'라는 정수 필드가 있다고 가정합니다.

herethere도 참조하십시오.

관련 문제