gpt4 book ai didi

android - ListView 显示奇怪的行为

转载 作者:行者123 更新时间:2023-11-30 02:36:09 25 4
gpt4 key购买 nike

我有什么

我的应用只有一个 Activity ,其他都是 fragment 其中一个 fragment 显示了一个 ListView ,该 ListView 是通过游标适配器从数据库中填充的。

我的问题

每次屏幕旋转或我在按下主页按钮后返回到应用程序时,我看到的是我有另一个 ListView 位于另一个 ListView 之上,即另一个相同的 ListView 也填充在现有 ListView 之上。

这是我的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasks_list_frag, container, false);
Log.d("HirakDebug", "tasksListFrag View Created");
tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
Cursor cursor = tasksDatabaseOperations.readData();
taskslist = (SwipeListView) view.findViewById(R.id.tasks_list);
adapter = new tasksCursorAdapter(getActivity(), cursor, CursorAdapter.FLAG_AUTO_REQUERY);
taskslist.setAdapter(adapter);
delete = (Button) view.findViewById(R.id.deleteAll);
delete.setOnClickListener(this);
taskslist.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
}

@Override
public void onClosed(int position, boolean fromRight) {
}

@Override
public void onListChanged() {
}

@Override
public void onMove(int position, float x) {
}

@Override
public void onStartOpen(int position, int action, boolean right) {
Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
}

@Override
public void onStartClose(int position, boolean right) {
Log.d("swipe", String.format("onStartClose %d", position));
}

@Override
public void onClickBackView(int position) {
Log.d("swipe", String.format("onClickBackView %d", position));
taskslist.closeAnimate(position);//when you touch back view it will close
}

@Override
public void onDismiss(int[] reverseSortedPositions) {

}

});


taskslist.setSwipeMode(SwipeListView.SWIPE_MODE_RIGHT); // there are five swiping modes
// there are four swipe actions
taskslist.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
taskslist.setOffsetRight(convertDpToPixel(80f)); // right side offset
taskslist.setAnimationTime(500); // Animation time
// enable or disable SwipeOpenOnLongPress

Log.d("HirakDebug", "tasksListFrag Adapter set");

return view;
}

public int convertDpToPixel(float dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}

@Override
public void onResume() {
super.onResume();
Log.d("HirakDebug", "taskListFrag resumed");
getRetainInstance();
}

@Override
public void setRetainInstance(boolean retain) {
super.setRetainInstance(true);
}

@Override
public void onPause() {
super.onPause();
Log.d("LifeCycle", "tLF Pause");
}

@Override
public void onStop() {
super.onStop();
Log.d("LifeCycle", "tLF Stop");
}

@Override
public void onDestroyView() {
super.onDestroyView();
Log.d("LifeCycle", "tLF DestroyView");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d("LifeCycle", "tLF Destroy");
}

@Override
public void onDetach() {
super.onDetach();
Log.d("LifeCycle", "tLF Detach");
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d("LifeCycle", "tLF Attach");
}

@Override
public void onStart() {
super.onStart();
Log.d("LifeCycle", "tLF Start");
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("LifeCycle", "tLF saveInstance State");
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public void onClick(View v) {
if (v == delete) {
Log.d("HirakDebug", "Delete Button Pressed");
tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
SQLiteDatabase sqLiteDatabase = tasksDatabaseOperations.getWritableDatabase();
sqLiteDatabase.delete(tasksDatabaseOperations.ASSIS_TASK_TABLE_NAME, null, null);
Log.d("HirakDebug", "tLF All Data Deleted");
adapter.notifyDataSetChanged();
Log.d("HirakDebug", "tLF ArrayList Cleared");
sqLiteDatabase.close();
}

}

最佳答案

只需检查您的 Activity onCreate 方法

if (saveInstance == null) {
mFragment = new Fragment();
fm.beginTransaction().replace(mFragment, TAG_TASK_FRAGMENT).commit();
}

关于android - ListView 显示奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com