ListViewのレイアウトを切り替えるためにカスタムSimpleCursorAdapterを実装しようとしていますが、スクロール中に非常にランダムな結果が出ます。ListViewがスクロールしているときにカスタムSimpleCursorAdapterが異なる結果を返すのはなぜですか?
ここで私の問題は、上下にスクロールすると、一見無作為にリストビューがレイアウトを混ぜることです。例えば、行は最初にlistview_item_rowレイアウトを持つことができますが、画面をスクロールしたりスクロールしたりすると、listview_item_reply_rowと置き換えられます。 newViewの仕組みを本当に理解しているとは言えません。私は正常にレイアウトビューで画像を隠すかどうかを判断するためにbindViewを使用することができましたが、新しいViewは暗黙のうちに実装されていて、なぜリストスクロールがそのように動作するのですか?
私の目標は、x個のアイテムでリストを作成することです。アイテムが返信か新しいメッセージかどうかに応じて、その行に特定のレイアウトをロードします。行にイメージがあるかどうかに応じて、行レイアウトでイメージビューを表示/非表示にしたいとします。
私がコードで省略したのは、インポートと行のレイアウトです。私は、v4サポートパッケージでFragmentsとSimpleCursorAdapterを使用してこれを実装しようとしています。 ListFragmentの行レイアウトは視覚的に異なりますが、同じウィジェットを含みます。
ListViewコントロールのレイアウト:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_feed_header_random"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:padding="4dp"
android:text="Allmänt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFF" />
<!--
The frame layout is here since we will be showing either
the empty view or the list view.
-->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/text_feed_header_random"
android:layout_above="@+id/footer" >
<!--
Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
<!-- Here is the view to show if the list is emtpy -->
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items."
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
<LinearLayout
android:id="@+id/footer"
style="@android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/button_random_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gör ett inlägg!" />
<Button
android:id="@+id/button_random_refresh"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Refresh list!" />
</LinearLayout>
</RelativeLayout>
上記のレイアウトを使用して凝縮フラグメント:ListFragmentが接続する
public class RandomFragment extends ListFragment implements LOG {
private DatabaseHelper mDbHelper;
private KarenfeedCursorAdapter mAdapter;
private Cursor mCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "RANDOMFRAGMENT START!");
mDbHelper = new DatabaseHelper(getActivity());
mDbHelper.open();
mDbHelper.setTable(Posts.TABLE_RANDOM_POSTS);
//TODO: Replace SimpleCursorAdapter with a FragmentList instead...
mCursor = mDbHelper.getAllPostsSortedCursor();
String[] columns = { Posts.COLUMN_ID, Posts.COLUMN_CREATED, Posts.COLUMN_USER, Posts.COLUMN_COMMENT };
int[] to = { R.id.imageItemPhoto, R.id.textItemDate, R.id.textItemUser, R.id.textItemComment };
int flags = 0;
mAdapter = new FeedCursorAdapter(getActivity(), R.layout.listview_item_row, mCursor, columns, to, flags);
this.setListAdapter(mAdapter);
initFeedList(); // This call in the end executes mCursor = mDbHelper.getAllPostsSorted(); mAdapter.changeCursor(mCursor); mAdapter.notifyDataSetChanged();
}
}
SimpleCursorAdapter:あなたの質問を読んだ後
public class FeedCursorAdapter extends SimpleCursorAdapter implements LOG {
private Context mContext;
private int mLayout;
public FeedCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
// TODO Auto-generated constructor stub
mContext = context;
mLayout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
Log.d(TAG, "id: " +id+ " parentId: " +parentId);
int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
if(id == parentId) {
view = inflater.inflate(R.layout.listview_item_row, parent, false);
} else {
view = inflater.inflate(R.layout.listview_item_reply_row, parent, false);
}
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "bindView()");
int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
String date = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_CREATED));
String user = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_USER));
String comment = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_COMMENT));
TextView dateView = (TextView) view.findViewById(R.id.textItemDate);
TextView userView = (TextView) view.findViewById(R.id.textItemUser);
TextView commentView = (TextView) view.findViewById(R.id.textItemComment);
ImageView imageView = (ImageView) view.findViewById(R.id.imageItemPhoto);
dateView.setText(date);
userView.setText(user);
commentView.setText(comment);
if(hasImage == 0) {
imageView.setVisibility(ImageView.GONE);
} else {
String bitmapPath = Environment.getExternalStorageDirectory().getPath() + "/feed/" + id + "_thumb.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imageView.setImageDrawable(bitmapDrawable);
imageView.setVisibility(ImageView.VISIBLE);
}
}
}
スクロールは、以前このアプリケーションをどのように構築したかで非常にうまく機能しました。 ArrayAdapterとArrayListを使用してListViewにデータを格納します。ただし、実装がどのように機能し、このSimpleCursorAdapter、Cursor、およびListFragmentがどのように機能し、何がうまくいかないのか、その振る舞いがあまりにも不安定なのかを正確に突き止めることはできません。やってみることができますか? ArrayListの使用を避けるためにカーソルを使用していますが、これは少なくとも必要ないはずです。 –
私は、このケースでCursorAdapterがどのように機能するかを幾分明確にしたと思いますが、あなたは確かにポイントを持っていました。あなたが説明したことを理解するためにこれを初めて知りました。誰でも、私はこれを私の質問に対する答えとして受け入れます。 –
私が言っていることは、いつでもuをスクロールアップまたはダウンするとアダプターと再び通信し、その時点でカーソルがリスト内のアイテムと比較して正しい位置にないことがあります。スクロールに関する別の結果。とにかく私は今日オフィスの後でこのコードを試して、私が得たものをあなたに知らせます。 –