【发布时间】:2013-09-13 19:25:03
【问题描述】:
我有一个使用带有选项卡导航的操作栏的应用程序,它通过自定义光标适配器显示列表视图,然后在单击列表视图项目时显示详细数据。当我的设备处于纵向时,一切正常。
当我的设备处于横向时出现问题。当我单击一个项目时,详细视图会在可见列表的顶部显示该项目。即:项目Accolate - Actonel 在列表中可见。无论我点击哪个项目,都会显示 Accolate。
截图:
这是我的 onListItemClick 方法:
public void onListItemClick(ListView parent, View v, int position, long id) {
mCurCheckPosition = position;
items.setSelectedPosition(position, cursor);
}
我正在为我的列表视图和详细视图使用片段。从纵向到横向的唯一区别是我在横向模式下连接到一个框架。这是我的适配器代码:
public class MyListAdapter extends SimpleCursorAdapter {
@SuppressWarnings("deprecation")
public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout , cursor, from, to);
}
public void setSelectedPosition(int position, Cursor cursor) {
// create data array
String [] data = new String[] {
cursor.getString(5),
cursor.getString(6),
cursor.getString(7),
cursor.getString(8),
cursor.getString(9),
cursor.getString(10),
cursor.getString(4)
};
// create the detail fragment
Bundle bundle = new Bundle();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
if (mDualPane == false) {
DetailFragment detailFragment = new DetailFragment();
bundle.putStringArray("Data", data);
detailFragment.setArguments(bundle);
ft.replace(android.R.id.content, detailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
} else {
DetailFragment detailFragment = new DetailFragment();
bundle.putStringArray("Data", data);
detailFragment.setArguments(bundle);
ft.replace(R.id.details, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// create the title textview
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(cursor.getString(4));
}
}
我在点击过程中记录了位置,它与光标中的正确记录匹配,但适配器显示的数据错误。
任何想法或建议将不胜感激。提前致谢。
【问题讨论】:
-
您是否为两个方向定义了 xml 布局?
-
是的,我有两个方向的 xml 布局,它们运行良好,因为我有一个双窗格,左侧是列表视图,右侧是详细信息。主要问题是横向显示错误的数据。
-
data 在不同的视图中是不同的??这让我对碎片产生了怀疑。如果您将它们拉出来并将它们设置为独立的测试活动会发生什么 - 使用其 xml 布局的问题?
-
我可以强制它一直处于“横向模式”,看看在纵向时我是否会得到相同的行为。我认为这是我的适配器的问题。
-
您的 OP 是否包含所有适配器代码??看起来它缺少一些东西。
标签: android android-listview simplecursoradapter android-listfragment android-cursoradapter