dimanche 20 novembre 2016

onItemToggled and checkbox how to implement - android recyclerview

I want to implement my onItemToggled so that when the user click the checkbox it will be mark as "checked" and then update the column DBContract.EntryColumns.IS_CHECKED to checked (set it to 1). Everything is working fine but the problem is I don't know how to implement onItemToggled method. Please can someone show me how to fix this.

Here is my Activity and list_item_book.xml where the CheckBox is defined.

public class MainActivity extends AppCompatActivity implements
        BookAdapter.OnItemClickListener,
        View.OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    private static final int BOOK_LOADER = 0;
    private BookAdapter mAdapter;
    private CheckBox mCheckBox;

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

        mAdapter = new BookAdapter(null);
        mAdapter.setOnItemClickListener(this);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        getLoaderManager().initLoader(BOOK_LOADER, null, this);
    }



    /* Click events for RecyclerView items */
    @Override
    public void onItemClick(View v, int position) {

        Intent intent = new Intent(MainActivity.this, DetailActivity.class);
        Uri currentBookUri = ContentUris.withAppendedId(DBContract.EntryColumns.CONTENT_URI, mAdapter.getItemId(position));
        intent.setData(currentBookUri);
        startActivity(intent);
    }



    /* This method gets called when I Click on checkboxes */
    @Override
    public void onItemToggled(boolean active, int position) {

        //what should I write here?
          Log.v(LOG_TAG, "INSIDE ON ITEM TOGGLED");

        Uri currentBookUri = ContentUris.withAppendedId(DBContract.EntryColumns.CONTENT_URI, mAdapter.getItemId(position));

        //  DBContract.EntryColumns.IS_CHECKED

    //

    }


    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
     //onCreateLoader code here
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Callback called when the data needs to be deleted
        mAdapter.swapCursor(null);
    }

}

//list_item_book.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeight"
    android:background="?android:attr/selectableItemBackground"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin">

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="@dimen/checkbox_padding" />

//
  //      rest of the code
</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire