CursorAdapterでのAndroidのnewViewとgetViewの動作について混乱します。 私がこれまでに見た実装のほとんどはgetViewを実装していますが、多くのオンラインソースはそれが道ではないことを示唆しています。例えばhereです。 bindView画像を設定しながらCursorAdapterがnewViewとbindViewの間で矛盾します
私の問題は、コードnewViewは、(画像の左または右に位置合わせを含む)のレイアウトを設定すること
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Decide if message was sent by Me or Other
View chatRow;
if(cursor.getInt(cursor.getColumnIndexOrThrow(DbChat.KEY_SENDER_ID)) == 0) {
chatRow = LayoutInflater.from(context.getApplicationContext())
.inflate(R.layout.listrow_chat_me,parent,false);
} else {
chatRow = LayoutInflater.from(context.getApplicationContext())
.inflate(R.layout.listrow_chat_other,parent,false);
}
return chatRow;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView chatText = (TextView) view.findViewById(R.id.chat_text);
ImageView imageView = (ImageView) view.findViewById(R.id.chat_img);
chatText.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbChat.KEY_TEXT)));
if(cursor.getInt(cursor.getColumnIndexOrThrow(DbChat.KEY_SENDER_ID)) == 0) {
imageView.setImageDrawable(ChatActivity.sIconBlue);
} else {
imageView.setImageDrawable(ChatActivity.sIconRed);
}
注の次のビットに要約(この場合には青または赤) 。 したがって、(色と位置の両方がカーソル上の同じIDチェックを照会するため)すべての赤い四角が左に、青の四角が右にあることが予想されます。 は代わりに、私は次のようなレイアウトを得る:
は基本的に、私の問題は、このquestionと同じですが、私は私の問題を解決するための提案のいずれかを見つけることができませんでした。
この奇妙な動作や問題の解決方法についてお返事いただき、ありがとうございます。デフォルトでは、ListView
(できるだけ頻繁にビューを再利用するCursorAdapter
トライを取り、他のソースによって
をオーバーライド調査! getItemViewType(int pos)メソッド内の個々のデータ要素にアクセスするために、getCursor()。moveToPosition(pos)を使用しました。 – untergam
'cursor =(Cursor)getItem(pos)'と 'getCursor()。moveToPosition(pos)'の間の効率については、コードに答えを追加しましたか? – untergam
どちらも機能します! [getItem()のコード](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/widget/CursorAdapter)に注目します。 .java#208)は非常に簡単です。 – ianhanniballake