ListView消失Android Studio

时间:2018-01-31 21:07:47

标签: java android listview android-studio

首先,我是java的初学者:) 我的项目是创建一个todolist。一切都已完成但在这里我有一点问题。我在互联网上搜索,但我找到的所有解决方案都没有做任何事情。

我的应用分为3个片段:我的“添加任务”部分,“待办事宜”部分和“完成”部分。

在“待办事项”和“完成”中,我有2个相应的列表视图。 我的问题是,当我进入“完成”部分时,我的对象就在其中,但是当我更改部分以返回“添加任务”然后返回“完成”时,这里再也没有了......我的listView不再出现。 另一方面关于“待办事项”清单无后顾之忧!

我真的很难理解发生了什么..我留下了一些代码,所以你可以提前帮助我!

My Done Section - >点击“添加任务” - > My add section - >然后返回“完成” - > Re- Done Section

我的DoneController:

public class DoneController extends Fragment {

private static ListView lv;
private static ArrayAdapter<String> mAdapter;
private static View rootView;
public static Handler handler = new Handler();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.done, container, false);
    lv = (ListView) rootView.findViewById(R.id.listview_done);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    handler.post(runnableCode);
}

public static Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        addTaskDone();
        handler.postDelayed(this, 1000);
    }
};
    public static void addTaskDone(/*String txt*/) {

    TaskDbHelper mHelper = addTaskController.getHelper();
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();
    Cursor cursor = db.query(TaskContract.TaskDone.TABLE_DONE,
            new String[]{TaskContract.TaskDone._ID, TaskContract.TaskDone.COL_TASK_DONE_TITLE},
            null, null, null, null, null);
    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(TaskContract.TaskDone.COL_TASK_DONE_TITLE); taskList.add(cursor.getString(idx));
    }
    if (mAdapter == null) {
       mAdapter = new ArrayAdapter<>(rootView.getContext(), R.layout.done, R.id.task_title2, taskList);
       lv.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }

    cursor.close();
    db.close();
}

}

注意:我的ToDoController对显示器没有问题:

public class TodoController extends  Fragment {

public static Handler handler = new Handler();
private static ArrayAdapter<String> mAdapter;
private static ListView mTaskListView;
public static View rootView;
public static Boolean isInit = false;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.todo, container, false);
   mTaskListView = (ListView) rootView.findViewById(R.id.listview_todo);
    rootView.setClickable(true);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    Button bttn_del = (Button) rootView.findViewById(R.id.task_delete);
    Button bttn_edit = (Button) rootView.findViewById(R.id.task_edit);

    bttn_edit.setVisibility(rootView.INVISIBLE);
    bttn_del.setVisibility(rootView.INVISIBLE);

    handler.post(runnableCode);


    mTaskListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Object o = mTaskListView.getItemAtPosition(i);
        }
    });

}

public static Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        updateUI();
        handler.postDelayed(this, 1000);
    }
};

public static void updateUI() {
    TaskDbHelper mHelper = addTaskController.getHelper();
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();

    Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
            new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
            null, null, null, null, null);

    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
        taskList.add(cursor.getString(idx));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<>(rootView.getContext(), R.layout.todo, R.id.task_title, taskList);

        mTaskListView.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }

    cursor.close();
    db.close();
}

0 个答案:

没有答案