Hello,
I've been hitting a wall with how to get a listener on the checkbox in
my listview. Could someone give me an example of how to do it in this
case? When this screen is displayed it pulls the data from the
database and displays in in a listview. The checkbox status is set in
filldata based on values from the database. I want to be able to
select/deselect checkboxes in the list and write the changes back to
the database as the end result. SO I'm thinking the first step is to
get a listener on the checkboxes. At this point I can't even click on
the list, but can click the checkbox on and off.  Here are my xml
layouts and java code. Thanks, Ryan.

createlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
      android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <LinearLayout android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

    <ListView android:id="@+id/android:list"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
        <TextView android:id="@+id/android:empty"
          android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="No Notes!"/>

      </LinearLayout>
</LinearLayout>

______________________________________________________

list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
 android:layout_width="wrap_content"
        android:layout_height="wrap_content">

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android";
                android:id="@+id/checkItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <!--  -->android:text="">-->
        </CheckBox>

<TextView android:id="@+id/desc" xmlns:android="http://
schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</TextView>
<TextView android:id="@+id/locat" xmlns:android="http://
schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></TextView>
</LinearLayout>

_________________________________________________________________________________

UpdateList.java
package shop.grocery.com;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class CreateList extends ListActivity{
                 private DbHelper mDbHelper;
         private static final int ACTIVITY_CREATE=0;

    {}
                @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.setContentView(R.layout.createlist);
                mDbHelper = new DbHelper(this);
                mDbHelper.open();
                fillData();
                registerForContextMenu(getListView());

            }//end onCreate


            private void fillData() {
                Toast.makeText(CreateList.this, "fillData called",
Toast.LENGTH_SHORT).show();
                Cursor createCursor = mDbHelper.fetchAllNotes();
                startManagingCursor(createCursor);

                // Create an array to specify the fields we want to display
in the list
                String[] from = new String[]{DbHelper.KEY_DESCRIPTION,
DbHelper.KEY_LOCATOR, DbHelper.KEY_CHECKED};

                // and an array of the fields we want to bind those fields to
                int[] to = new int[]{R.id.desc, R.id.locat, R.id.checkItem};

             // Now create an array adapter and set it to display using our
row
                SimpleCursorAdapter item = new SimpleCursorAdapter(this,
R.layout.list_row, createCursor, from, to);
                item.setViewBinder(new SimpleCursorAdapter.ViewBinder()
{
                public boolean setViewValue(View view, Cursor cursor, int
columnIndex) {
                        int nCheckedIndex = cursor.getColumnIndexOrThrow
(DbHelper.KEY_CHECKED);
                        if (columnIndex == nCheckedIndex) {
                                CheckBox cb = (CheckBox) view;
                                boolean bChecked = 
(cursor.getInt(nCheckedIndex) !=
0);
                                cb.setChecked(bChecked);
                                return true;        }
                            return false;      }    });
                        setListAdapter(item);

            }//end fill data


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

            @Override  //clicks not recognized
            protected void onListItemClick(ListView l, View v, int position,
long id){
                super.onListItemClick((ListView) l.findViewById(R.id.checkItem),
v, position, id);
                Toast.makeText(CreateList.this, "onlistitemclick",
Toast.LENGTH_SHORT).show();
        }


}//end class
/*code I've been experimenting with unsuccessfully
//getListView().setOnItemClickListener((OnItemClickListener) this);
//       mCheckedText = (EditText) findViewById(R.id.checked);
//       mRowId = savedInstanceState != null ?
savedInstanceState.getLong(DbHelper.KEY_ROWID)
//                              : null;
//        if (mRowId == null) {
//                      Bundle extras = getIntent().getExtras();
//                      mRowId = extras != null ? 
extras.getLong(DbHelper.KEY_ROWID)
        //                                                              : null;
        //      }
final CheckBox checkbox = (CheckBox)findViewById(R.id.checkItem);
   checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
        // Perform action on clicks
        if (checkbox.isChecked()) {
                Toast.makeText(CreateList.this, "Selected",
Toast.LENGTH_SHORT).show();
                } else {
                        Toast.makeText(CreateList.this, "Not selected",
Toast.LENGTH_SHORT).show();
                }    }}); */

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to