1

ユーザーがリストをスクロールできるように AlphabetIndexer を実装しようとしていますが、アプリを実行してもリストに何も表示されません。誰かが理由を教えてください。

注: Adapter のコンストラクターで AlphabetIndexer をインスタンス化していないのは、その時点で Cursor が使用できないためです。

関連するコードは次のとおりです。

アクティビティの onCreate() メソッドで:

mList = (ListView)findViewById(R.id.mylist);
mList.setOnItemClickListener(this);
mList.setFastScrollEnabled(true);
mAdapter = new MyAdapter(MyActivity.this, R.layout.layout_list_row, null, new String[] {MyColumns.NAME}, new int[] {R.id.itemname});
mList.setAdapter(mAdapter);
mList.setFastScrollEnabled(true);
doQuery();

doQuery() は、AsyncQueryHandler を使用して Cursor を照会するメソッドです。AsyncQueryHandler は次のようになります。

private final class MyQueryHandler extends AsyncQueryHandler {
    public MyQueryHandler(Context context) {
        super(context.getContentResolver());
    }
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
    if (!isFinishing()) {
        if (mAdapter != null) {
            mAdapter.changeCursor(cursor);
        }
    }
    else {
        cursor.close();
    }
}

}

最後に、SimpleCursorAdapter です。不要な部分を取り出しました:

public class MyAdapter extends SimpleCursorAdapter implements View.OnClickListener {

    private Cursor mCursor;
    AlphabetIndexer alphaIndexer;


public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
}

public int getPositionForSection(int section) {
    return alphaIndexer.getPositionForSection(section);
}

public int getSectionForPosition(int position) {
    return alphaIndexer.getSectionForPosition(position);
}

public Object[] getSections() {
    return alphaIndexer.getSections();
}

public void onClick(View v) {
    // ...
}       

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // ...
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // ...
}

@Override
public void changeCursor(Cursor cursor) {
    super.changeCursor(cursor);

    if (MyActivity.this.mCursor != null) {
        stopManagingCursor(MyActivity.this.mCursor);
        MyActivity.this.mCursor.close();
        MyActivity.this.mCursor = null;
        mCursor = null;
    }
    MyActivity.this.mCursor = cursor;
    startManagingCursor(MyActivity.this.mCursor);
    mCursor = cursor;
    alphaIndexer = new AlphabetIndexer(mCursor, mCursor.getColumnIndex(MyColumns.NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    alphaIndexer.setCursor(mCursor);
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    return doQuery();
}

}

4

2 に答える 2

3

リストが高速スクロールを保証するほど長くない場合、Android は高速スクロール機能を非表示にすることがあります。それがあなたの問題かどうかはわかりませんが、リストにたくさんの項目を追加してみる価値があるかもしれません.

于 2012-08-07T22:00:27.943 に答える