0

入力しようとしているリスト ビューを持つアクティビティを実行すると、空として表示されます。ただし、logcat では、カスタム 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"));
}

}

中括弧を許してください。間違ってコピーした可能性があります。

4

1 に答える 1

1

CursorAdapter の考え方は、コードからすべてのポジショニングをカプセル化し、視覚化だけを任せることです。つまり、moveToNext やその他の配置方法を使用する必要はありません。

コードでわかるように、フィルタリングを実装しようとしています。しかし、データベースを操作するという考えは、SQL クエリで基準を定式化することです。すべてのジョブを照会する代わりに、ListView に表示する必要があるジョブのみを選択する必要があります。

bindView と newView は、カーソルが指している現在のレコードでのみ機能する必要があり、moveToNext または close を呼び出してはなりません。また、CursorAdapter は、カーソルに「_id」という名前の整数フィールドがあると想定しています。

あちらこちらもご覧ください

于 2011-10-29T19:24:00.637 に答える