2011-10-29 13 views
0

リストビューを持つアクティビティを実行すると、作成しようとしている空の表示されます。しかし、logcatでは、カスタムCursorAdapterがデータを通過し、少なくともいくつかのビューを返すことがわかります。カスタムCursorAdapterにListViewが設定されていません

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'という名前の整数フィールドを持っていることを前提としています。

hereおよびthereも参照してください。

関連する問題