LoaderCallbacks不会更新ListView

时间:2018-04-06 16:42:20

标签: java android listview

我正在编写一个拥有sqlite数据库的小应用程序。我的主要列表视图 活动。我正在使用游标适配器来显示列表中的项目,它工作正常。每次我打开应用程序时,列表都包含所有必需的信息。当我尝试使用LoaderCallback以便每次更改数据库时更新列表视图,但列表视图仅在我关闭并重新打开应用程序时更改。

这是活动代码:

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

private CursorAdapter taskItemListCursorAdapter;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //testReset(getApplicationContext());

    //set list to show items
    taskItemListCursorAdapter = new CursorAdapterListIem(getApplicationContext(),null);
    listView = (ListView)findViewById(R.id.listView_list_of_tasks);
    listView.setAdapter(taskItemListCursorAdapter);
    this.getLoaderManager().initLoader(1, null,this );




    //getContentResolver().registerContentObserver(DBContract.TASKS_TABLE_URI,false,taskItemListCursorAdapter.registerDataSetObserver(null));

   //this.createDBtest();

    //bind listener new task
    FloatingActionButton newGoalButton = (FloatingActionButton)findViewById(R.id.main_activity_floatingButton_add_new_goal);
    newGoalButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent addNewGoalIntent = new Intent(getApplicationContext(),CreateNewGoal.class);
            getApplicationContext().startActivity(addNewGoalIntent);
        }
    });

    //listView listeners
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent goalTimerIntent = new Intent(view.getContext(),GoalTimer.class);
            goalTimerIntent.setData(Uri.withAppendedPath(DBContract.TASKS_TABLE_URI,String.valueOf(id)));
            getApplicationContext().startActivity(goalTimerIntent);
        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            final long finalId=id;

            //create listner for yes or no
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            //Yes button clicked
                            getContentResolver().delete(Uri.withAppendedPath(DBContract.TASKS_TABLE_URI,String.valueOf(finalId)),null,null);
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            break;
                    }
                }
            };

            //create new alert listener
            AlertDialog.Builder deleteDialog = new AlertDialog.Builder(MainActivity.this);
            deleteDialog.setTitle("Delete Goal");
            deleteDialog.setMessage("Are you sure you want to delete this goal?");
            deleteDialog.setPositiveButton("YES",dialogClickListener);
            deleteDialog.setNegativeButton("NO",dialogClickListener);
            deleteDialog.create();
            deleteDialog.show();

            return false;
        }
    });
}

@Override
public Loader onCreateLoader(int id, Bundle args) {
    return new CursorLoader(getApplicationContext(),DBContract.TASKS_TABLE_URI,null,null,null,null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (loader==null) {
        Cursor c = getContentResolver().query(DBContract.TASKS_TABLE_URI,null,null,null,null);
        taskItemListCursorAdapter = new CursorAdapterListIem(this, c);
    }
    else
        taskItemListCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader loader) {
    taskItemListCursorAdapter.swapCursor(null);
}
}

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ariel.weeklytimer.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">


    <ListView
        android:id="@+id/listView_list_of_tasks"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="true" />

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/main_activity_floatingButton_add_new_goal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="@dimen/fab_margin"
    android:scaleType="center"
    android:src="@mipmap/ic_add_circle_black_48dp"
    app:backgroundTint="@color/colorPrimary"
    app:borderWidth="0dp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp" />



</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

onLoadFinished()尝试在您的适配器上调用notifyDataSetChanged()

taskItemListCursorAdapter.notifyDataSetChanged();
相关问题