dimanche 24 janvier 2016

using a checkbox in other activity in android

I have created an XML for a listview(activity_main.xml) and another XML for its row(custom_row.xml).I am using SimpleCursorAdapter to fetch the data to the listview.Now I want to use a checkbox which I created in custom_row.xml into the MainActivity. How to do this ?

My code:

custom_row.xml


  <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/title"
        android:textSize="17dp"
        android:textColor="#008080"
        android:layout_marginTop="8dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:textSize="15dp"
        android:layout_marginTop="3dp"
        android:textStyle="italic"
        android:textColor="#6d7b8d"
        android:id="@+id/date"
        android:layout_below="@+id/title"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="11dp"
        android:layout_below="@+id/date"
        android:background="#b6b6b4"
        android:id="@+id/view" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:layout_marginTop="5dp"
        android:focusable="false"
        android:visibility="invisible"
        android:layout_alignTop="@+id/title"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    </RelativeLayout>

activity_main.xml

     <RelativeLayout   xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:id="@+id/notesList"
        android:layout_alignBottom="@id/android:empty" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/add"
        android:background="@drawable/add"

        android:clickable="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Empty"
        android:textStyle="bold"
        android:textSize="15dp"
        android:textColor="#008080"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="150dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:visibility="invisible"
        android:id="@+id/listitem_delete"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    </RelativeLayout>

MainActivity.java

            package com.example.hp.notes;

        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.content.res.Resources;
        import android.database.Cursor;
        import android.graphics.Typeface;
        import android.support.v4.widget.SimpleCursorAdapter;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.util.SparseBooleanArray;
        import android.view.ContextMenu;
        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuInflater;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.Button;
        import android.widget.CheckBox;
        import android.widget.CompoundButton;
        import android.widget.ImageView;
        import android.widget.ListView;
        import android.widget.TextView;
        import android.widget.Toast;

        import java.util.ArrayList;

        public class NotesListActivity extends AppCompatActivity {

            ListView notes;
            Button delete;
            CheckBox cb;
            private Typeface mCustomFont;
            SparseBooleanArray mCheckStates ;
            SimpleCursorAdapter notes_cur;
            private int mNoteNumber = 1;
            private static final int ACTIVITY_CREATE=0;
            private static final int ACTIVITY_EDIT=1;

            private NotesDbAdapter mDbHelper;
            ImageView add;
            public  NotesListActivity notesListActivity = null;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                notes=(ListView)findViewById(R.id.notesList);
                delete=(Button)findViewById(R.id.listitem_delete);
                add=(ImageView)findViewById(R.id.add);
                notes.setEmptyView(findViewById(android.R.id.empty));
                notesListActivity = this;
                mDbHelper = new NotesDbAdapter (this);
                mDbHelper.open();
                fillData();
              //  setListData();

                Resources res =getResources();
                notes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent i = new Intent(NotesListActivity.this, NotesEditActivity.class);
                        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
                        startActivityForResult(i, ACTIVITY_EDIT);
                    }
                });

                add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createNote();
                    }
                });

                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SparseBooleanArray checkedItemPositions = notes.getCheckedItemPositions();
                        int itemCount = notes.getCount();

                        for(int i=itemCount-1; i >= 0; i--){
                            if(checkedItemPositions.get(i)){
                                mDbHelper.deleteNote(View.NO_ID);
                            }
                        }
                        checkedItemPositions.clear();
                        notes_cur.notifyDataSetChanged();;
                    }
                });


                registerForContextMenu(notes);


            }



            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                if (v.getId() == R.id.notesList) {
                    super.onCreateContextMenu(menu, v, menuInfo);
                    menu.setHeaderTitle("Select");
                    MenuInflater mi = getMenuInflater();
                    mi.inflate(R.menu.list_menu_item_longpress, menu);
                    menu.add(0, v.getId(), 0, "Edit");
                    menu.add(0, v.getId(), 0, "Hide");
                    menu.add(0,v.getId(),0,"Change Priority");
                }
            }
            @Override
            public boolean onContextItemSelected(MenuItem item) {
                switch(item.getItemId()) {
                    case R.id.menu_delete:
                        final AdapterView.AdapterContextMenuInfo info =
                                (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

                        // Setting Dialog Title
                        alertDialog.setTitle("Confirm Delete...");

                        // Setting Dialog Message
                        alertDialog.setMessage("Are you sure you want delete this?");

                        // Setting Icon to Dialog
                        alertDialog.setIcon(R.drawable.delete);

                        // Setting Positive "Yes" Button
                        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                mDbHelper.deleteNote(info.id);
                                fillData();
                            }
                        });

                        // Setting Negative "NO" Button
                        alertDialog.setNegativeButton("NO", new    DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to invoke NO event
                                dialog.cancel();
                            }
                        });

                        // Showing Alert Message
                        alertDialog.show();

                        return true;
                }
                return super.onContextItemSelected(item);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(final MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.delete_note) {
                    for(int i = 0; i < notes.getChildCount(); i++) {

                        notes.getChildAt(i).findViewById(R.id.checkBox).setVisibility(View.VISIBLE);

                    }

                }

                return super.onOptionsItemSelected(item);
            }


            private void fillData() {
                // Get all of the notes from the database and create the item list
                Cursor notesCursor = mDbHelper.fetchAllNotes();
                startManagingCursor(notesCursor);


                String[] from = new String[] { NotesDbAdapter.KEY_TITLE ,NotesDbAdapter.KEY_DATE};
                int[] to = new int[] { R.id.title ,R.id.date};


                // Now create an array adapter and set it to display using our row
                SimpleCursorAdapter notes_cur =
                        new SimpleCursorAdapter(this, R.layout.custom_row, notesCursor, from, to);
                notes.setAdapter(notes_cur);
            }


            private void createNote() {
                Intent i = new Intent(this, NotesEditActivity.class);
                startActivityForResult(i, ACTIVITY_CREATE);
            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
                super.onActivityResult(requestCode, resultCode, intent);
                fillData();
            }


        }




Aucun commentaire:

Enregistrer un commentaire