NullPointerException while traversing a ListView - android

Code:
SimpleCursorAdapter ada = new SimpleCursorAdapter(this,
R.layout.custom_layout, ssCursor, new String[] {
"String" }, new int[] {
R.id.txt2 });
lvSms.setAdapter(ada);
btnDel.setOnClickListener(new View.OnClickListener() {
private ArrayList<String> msgArr;
public void onClick(View v) {
LinearLayout ly = (LinearLayout) findViewById(R.id.lv);
ListView lvMsg = (ListView) ly.getChildAt(3);
int listCount = lvSms.getCount();
for (int a = 0 ; a < listCount ; a++)
{
LinearLayout ll = (LinearLayout) lvMsg.getChildAt(a);
LinearLayout l2 = (LinearLayout) ll.getChildAt(0);
LinearLayout l3 = (LinearLayout) l2.getChildAt(0);
CheckBox chkBox =(CheckBox) l3.getChildAt(0);
boolean valueOfChkBox = chkBox.isChecked();
if (valueOfChkBox) {
LinearLayout l4 = (LinearLayout) l2.getChildAt(1);
TextView txt1 = (TextView) l4.getChildAt(0);
msgArr = new ArrayList<String>();
msgArr.add(txt1.getText().toString());
Log.i("hello", txt1.getText().toString());
}
} });
I am getting a NullPointerException, as getChildAt returns the visible rows and I have to also check the invisible rows, either they are checked or not. So how could I check it on separate button?

I'm guessing that you want to obtain the text of the TextViews from the rows that are checked in the ListView. You can't use getChildAt() and just parse all the rows, instead you should use the data that you pass in the adapter, the cursor(and figure out what rows are checked). Because you use a custom row for the layout you'll have to somehow maintain the checked/unchecked status of your rows CheckBoxes(with a custom adapter). When is time to get the text, at a click of that Button, then simply find out what rows are currently checked and extract from the cursor directly the text you want.
A simple way to store the status of the CheckBoxes is to have an ArrayList<Boolean> that you'll modify depending on which CheckBox you act(probably not that efficient):
private class CustomAdapter extends SimpleCursorAdapter {
// this will hold the status of the CheckBoxes
private ArrayList<Boolean> checkItems = new ArrayList<Boolean>();
public CustomAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
int count = c.getCount();
for (int i = 0; i < count; i++) {
checkItems.add(false);
}
}
//this method returns the current status of the CheckBoxes
//use this to see what CheckBoxes are currently checked
public ArrayList<Boolean> getItemsThatAreChecked() {
return checkItems;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
int position = cursor.getPosition();
CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox);
ckb.setTag(new Integer(position));
ckb.setChecked(checkItems.get(position));
ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Integer realPosition = (Integer) buttonView.getTag();
checkItems.set(realPosition, isChecked);
}
});
}
}
Then parse the ArrayList you get from getItemsThatAreChecked() and extract the data from the cursor. Here you have a small example http://pastebin.com/tsZ6mzt9 (or here git://gist.github.com/2634525.git with the layouts)

LinearLayout ly = (LinearLayout) findViewById(R.id.lv);
The above line - does it try to take the root layout? If so, you can't use findViewById. Rather use:
getLayoutInflater().inflate(R.layout.mylayout)
getLayoutInflater() is Activity's method

Related

Android How to show/hide all checkbox by button and by long press CustomAdapter

I want to do that when I press and hold on an item or I click a button, all CheckBox in ListView Show / Hide depending on the situation it is now. I have a CustomAdapter in the Array List MY CustomAdapter
public class MyCustomCursorAdapter extends CursorAdapter {
private Context mContext;
private String[] mColumns;
private int[] mViews;
private int mLayout;
private boolean[] mCheckBoxStates;
private int mCheckBoxView;
protected int mCountCheckBox;
private CheckBox checkbox1;
// Constructor for the Custom Cursor Adapter
MyCustomCursorAdapter(Context context, int layout, Cursor csr, String[]
from_columns, int[] to_views, int checkbox_view) {
super(context, csr, false);
mContext = context;
mLayout = layout;
mColumns = from_columns;
mViews = to_views;
mCheckBoxView = checkbox_view;
}
#Override
// Inflate the layout we are going to use (as passed via 2nd parameter)
public View newView(Context context, Cursor csr, ViewGroup parent) {
// Initialise an int array for the checkboxes (all will be 0)
mCheckBoxStates = new boolean[csr.getCount()];
mCountCheckBox = csr.getCount();
return LayoutInflater.from(context).inflate(mLayout, parent, false);
}
#Override
// Tie the from_columns to the display views
public void bindView(View view, Context context, Cursor csr) {
final Cursor fcsr = csr;
// Place the data from the cursor into the respective View (TextView)
for (int i = 0; i < mColumns.length; i++) {
((TextView)view.findViewById(mViews[i])).setText(csr.getString
(csr.getColumnIndex
(mColumns[
i])));
}
// Set the checkbox (note should be false, unless otherwise changed)
CheckBox currentCheckBox = (CheckBox) view.findViewById(mCheckBoxView);
currentCheckBox.setChecked(mCheckBoxStates[csr.getPosition()]);
currentCheckBox.setTag(new
Long(csr.getLong(csr.getColumnIndex(DatabaseHandler.KEY_ID))));
//
currentCheckBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
final int position = fcsr.getPosition();
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
// Store the checkbox status
mCheckBoxStates[position] = ((CheckBox) buttonView).isChecked();
int restore_cursor_position = fcsr.getPosition();
//Move the Cursor to the respective row
//NOTE! 1st position in Lisview is 1 but equates to cursor row 0
etc hence -1
fcsr.moveToPosition(position);
Toast.makeText(mContext,
"You Changed the CheckBox for row " +
Integer.toString(position + 1) +
" Item is " +
fcsr.getString(fcsr.getColumnIndex(DatabaseHandler.KEY_ITEM))
,
Toast.LENGTH_SHORT
).show();
//restore the Cursor's position
fcsr.moveToPosition(restore_cursor_position);
}
});
}
// get the list of items (the ID's as long) that have been checked.
public long[] getCheckedRecordIdList() {
Cursor csr = this.getCursor();
// using ArrayList as we can add as we don't yet know how many
ArrayList<Long> rva = new ArrayList<>();
// Just in case save the current position of the Cursor
int restore_cursor_position = csr.getPosition();
// Loop through the checkbox states
for (int i = 0; i < mCheckBoxStates.length; i++) {
// If the checkbox reflected as being checked then handle, else
ignore it
if (mCheckBoxStates[i]) {
// Move to the respective cursor row
csr.moveToPosition(i);
// get the respective ID and store in the arraylist
rva.add(csr.getLong(csr.getColumnIndex(DatabaseHandler.KEY_ID)));
}
}
// Done with the Cursor so re-position it
csr.moveToPosition(restore_cursor_position);
// Create the long array to be returned
long[] rv = new long[rva.size()];
// Populate the long array with the id's from the arraylist
for (int i = 0; i < rva.size(); i++) {
rv[i] = rva.get(i);
}
// return the long[]
return rv;
}
}
And by the way how do I do when I click the Repeat button it will make a refresh to TextView (normal, not in the array and found in previous Activity)

Dynamic row layout for ListView

I have a ListView where I send data from a database using SimpleCursorAdapter, but I need to have dynamic row layout, because every table can have different number of columns.
Let's say I create TextViews according to the number of columns:
public int getColumnNumbers() {
int i = 0;
cursor = db.rawQuery("PRAGMA table_info("+DATABASE_TABLE_NAME+")", null);
if (cursor.moveToFirst()) {
do {
i++;
} while (cursor.moveToNext());
}
cursor.close();
return i;
}
........
int rowsNumber = getColumnNumbers();
textViews = new TextView[rowsNumber];
for(i = 0; i < rowsNumber; i++) {
textViews[i] = new TextView(this);
textViews[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
What I'm basically looking for, is a way to get these TextViews passed to CursorAdapter's (or other adapter's) argument int[] to
I'm pretty new to this, so I would appreciate any help or advice.
EDIT:
I'm adding my implementation of bindViewmethod, which I made with help of the links provided here, in case someone would have to face similar issue in the future.
#Override
public void bindView(View view, Context context, Cursor cursor) {
int i, count = myHelper.getColumnNumbers();
String[] columnNames = myHelper.getColumnNamesString();
String text;
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout_horizontal);
for(i = 0; i < (count-1); i++) {
TextView textView = new TextView(context);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
text = cursor.getString(cursor.getColumnIndexOrThrow(columnNames[i+1]));
textView.setText(text);
layout.addView((TextView)textView);
}
}
EDIT 2:
I found out yesterday that the implementation mentioned above works until you need to scroll. After scrolling, ListView gets deformed.
So again, in case someone would like to do something similar, I'm adding my whole Adapter class.
public class DatabaseCursorAdapter extends CursorAdapter {
private DatabaseHelper myHelper;
private int count;
private String[] columnNames;
public DatabaseCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
myHelper = new DatabaseHelper(context);
count = myHelper.getColumnNumbers();
columnNames = myHelper.getColumnNamesString();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.text_select, parent, false);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout_horizontal);
int i;
for(i = 0; i < count; i++) {
TextView textView = new TextView(context);
textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
textView.setTag(Integer.valueOf(i));
layout.addView(textView);
}
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
int i;
String text;
for(i = 0; i < count; i++) {
TextView textView = (TextView) view.findViewWithTag(Integer.valueOf(i));
text = cursor.getString(cursor.getColumnIndexOrThrow(columnNames[i]));
textView.setText(text);
}
}
}
In the and, as it is with most issues, the solution is pretty easy. Technique is almost the same as if you would use a static layout, except instead of using findViewById, you just tag elements of your layout and use findViewWithTag method.
You need to look at good tutorial, a link is
Populating a ListView with a CursorAdapter . The web page has some nice explanations.
Look at TodoCursorAdapter and the bindView method to check which column/data is available from the database. Snippet of code from tutorial:
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
// Extract properties from cursor
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
// Populate fields with extracted properties
tvBody.setText(body);
}
I think this is a simple code design.
Code in the webpage to populate data onto Listview:
// Find ListView to populate
ListView lvItems = (ListView) findViewById(R.id.lvItems);
// Setup cursor adapter using cursor from last step
TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView
lvItems.setAdapter(todoAdapter);
You can implement an ArrayAdapter instead of CursorAdapter. This makes sense if your app is not continually interfacing with the database. The link is Using an ArrayAdapter with ListView . It is the same website.
In this case, look at getView method instead of the similar bindView.

Wrong behavior in a button located in each row of a list when I change the orientation of a tablet

I have a custom SimpleCursorAdapter and a list view. Each row of the list have a name and a button. When I press the button for each name, a dialog appears with a description.
Inside the custom SimpleCursorAdapter I set the onclick method for the button. When I have a large list, my listView gets a scroll bar. And I dont know why, when I scroll down, the last rows of my list doesnt show the correct description for each row. This is my code:
public class listServicesCursorAdapter extends SimpleCursorAdapter{
private Context context;
private int layout;
private String[] from;
private int[] to;
public listServicesCursorAdapter (Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
this.from = from;
this.to = to;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
//Column of BD that we want to recover
String column = null;
//Index of the column of DB
int nameCol = 0;
//Result of obtain the index of the column of DB
String nombre = null;
//Name of the textView in the Layout where we want to show the result
TextView name_text= null;
String description = null;
String nameService = null;
//For each value of DB, we show it in the text view.
for (int i=0; i<from.length; i++){
column= from[i];
nameCol = cursor.getColumnIndex(column);
name = cursor.getString(nameCol);
//the values to[i] equals to 0 indicates values that we need but
//that we are not showing in the list directly
//0 -> description
if(to[i] == 0){
description = name;
}else{
nameService = name;
name_text = (TextView) v.findViewById(to[i]);
if (name_text != null) {
name_text.setText(name);
}
}
}
ImageButton buttonDescription = (ImageButton) v.findViewById(R.id.imageButtonDescription);
//we store in a bundle the name and description of the service, so we can use it in
// the setOnClickListener method.
final Bundle mArguments = new Bundle();
mArguments.putString("name", nameService);
mArguments.putString("description", description);
buttonDescription .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setMessage(mArguments.getString("description"))
.setTitle(mArguments.getString("name"))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
return v;
}
}
This is where I call the adapter:
ServiceSqliteDao serviceDao = new ServiceSqliteDao();
//get the services for DB
Cursor mCursorServices = serviceDao.listServices(getActivity());
if(mCursorServices.getCount()>0){
//indicate the fields we want to show (from) and where (to)
String[] from = new String[] { "name", "description"};
int[] to = new int[] { R.id.checkBoxService,0};
ListView lvServices = (ListView) v.findViewById (R.id.listViewServices);
ListServicesCursorAdapter notes = new ListServicesCursorAdapter (getActivity(), R.layout.activity_file_service, mCursorServices, from, to, 0);
lvServices.setAdapter(notes);
Why do I get this behavior?. I get all the names in the list right but when I press the button in horizontal way (I mean a put the tablet horizontally) and get the scroll bar in my list, I dont get the right description. By the other hand, if I use the tablet vertically, I dont get the scroll bar in my list and I get the right description in each button.
This is my layout:
<ListView
android:id="#+id/listViewServices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
SOLUTION:
newView should look like this:
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
and bindView should look like this:
#Override
public View bindView(View v, Context context, Cursor cursor) {
//Column of BD that we want to recover
String column = null;
//Index of the column of DB
int nameCol = 0;
//Result of obtain the index of the column of DB
String nombre = null;
//Name of the textView in the Layout where we want to show the result
TextView name_text= null;
String description = null;
String nameService = null;
//For each value of DB, we show it in the text view.
for (int i=0; i<from.length; i++){
column= from[i];
nameCol = cursor.getColumnIndex(column);
name = cursor.getString(nameCol);
//the values to[i] equals to 0 indicates values that we need but
//that we are not showing in the list directly
//0 -> description
if(to[i] == 0){
description = name;
}else{
nameService = name;
name_text = (TextView) v.findViewById(to[i]);
if (name_text != null) {
name_text.setText(name);
}
}
}
/********************************NEW CODE ************************************/
String uniMedition = cursor.getString(cursor.getColumnIndex("unitMedition"));
if(uniMedition.equals("none")){
EditText etMedida = (EditText) v.findViewById(R.id.editTextMedida);
etMedida.setVisibility(View.INVISIBLE);
TextView tvUniMedition = (TextView) v.findViewById(R.id.textViewUniMedition);
tvUniMedition .setVisibility(View.INVISIBLE);
}else{
EditText etMedida = (EditText) v.findViewById(R.id.editTextMedida);
etMedida.setVisibility(View.VISIBLE);
TextView tvUniMedition = (TextView) v.findViewById(R.id.textViewUniMedition);
tvUniMedition .setVisibility(View.VISIBLE);
tvUniMedition .setText(uniMedition);
}
/********************************END NEW CODE ************************************/
ImageButton buttonDescription = (ImageButton) v.findViewById(R.id.imageButtonDescription);
//we store in a bundle the name and description of the service, so we can use it in
// the setOnClickListener method.
final Bundle mArguments = new Bundle();
mArguments.putString("name", nameService);
mArguments.putString("description", description);
buttonDescription .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setMessage(mArguments.getString("description"))
.setTitle(mArguments.getString("name"))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
}
}
Now everything works fine!.
Why do I get this behavior?. I get all the names in the list right but
when I press the button in horizontal way (I mean a put the tablet
horizontally) and get the scroll bar in my list, I dont get the right
description.
When your ListView doesn't have space to show all of the rows it will recycle the row view for performance reasons. The problem is that in your SimpleCursorAdapter you override the newView() method which will be called only when the ListView doesn't have a recycled view. Override bindView() to do the work as that method is called for each row, in the newView() method just inflate/build the row layout.

CheckedTextViews will randomly appear checked in a list if I click one further up the list

Ok, so this has been somewhat addressed alot on this site, however I do not believe the exact problem with what my code uses. I am filling a listView with CheckedTextViews which works completely. However when I click on an item it gets checked but when I scroll up and down random rows are also checked. I realize it must have something to do with how the ListView keeps track of the items. I am running into some errors at the moment. I attempted to fill a hashmap with the list of the rows so I can keep track which one is set to true and which are false. However I am not positive where to implement the map and try to fill it.
Here is my OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmenu);
//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.toppingCheckedTextView };
for(int i=0; i< from.length; i++){
map.put(i, false);
}
contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}
I attempt to place the map in the onCreate to fill it however it complains about a nullpointer.
Here is where I tried using the OnListItemClick method
#Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3){
final int index = arg2 - arg0.getFirstVisiblePosition();
View v = arg0.getChildAt(index);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.toppingCheckedTextView);
if((Boolean)map.get(index) == true){
ctv.setChecked(true);
ctv.setVisibility(View.VISIBLE);
} else{
ctv.setVisibility(View.GONE);
}
}
I have read alot on this, and it seems that alot of solutions involves using getView(), however I don't know if that applies to my situation. Any help would be greatly appreciated!
First of all do you need a SimpleCursorAdapter? You set the adapter with a null cursor:
contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to); // the third parameter is the cursor and you set it to null!
The behavior you see it's because of the ListView is recycling views and yes you'll have to implement your own adapter and override bindView(). The code bellow is based on another answer to a similar question maybe you'll want to look at it( Getting the selected View from ListView ). Here is an example:
public class TestCursorAdapter extends ListActivity {
MySimpleAdapter adapter;
private HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] columns = new String[] { "_id", "name" };
MatrixCursor mc = new MatrixCursor(columns); // cursor for testing
for (int i = 1; i < 35; i++) {
long id = i;
mc.addRow(new Object[] { id, "Name" + i });
}
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.checked_text };
adapter = new MySimpleAdapter(this,
R.layout.adapter_mysimpleadapter_row, mc, from, to);
setListAdapter(adapter);
}
private class MySimpleAdapter extends SimpleCursorAdapter {
public MySimpleAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
CheckedTextView ctv = (CheckedTextView) view
.findViewById(R.id.checked_text);
long pos = cursor.getLong(0); // the id from the cursor
if (positionHide.get(pos) == null) {
ctv.setChecked(false);
// we don't have this id in the hashmap so the value is by
// default false, the TextView is GONE
} else {
// we have the value in the Hashmap so see what it is and set
// the textview visibility from this value
Boolean tmp = positionHide.get(pos);
if (tmp.booleanValue()) {
ctv.setChecked(true);
} else {
ctv.setChecked(false);
}
}
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Boolean tmp = positionHide.get(id);
if (tmp == null) {
// if null we don't have this key in the hashmap so
// we add it with the value true
positionHide.put(id, true);
} else {
positionHide.put(id, !tmp.booleanValue());
// if the value exists in the map then inverse it's value
}
adapter.notifyDataSetChanged(); // notify the adapter that something has
// changed
}
}

Getting checkbox information from a listview

I have an activity with a list fragment in it that has a layout with a checkbox for each row. I set the onclick xml attribute for the checkbox and do the following for testing
public void onBoxClick(View v){
checkedItems = listview.getCheckedItemPositions();
int checkedItemsCount = checkedItems.size();
}
checkedItemsCount comes back 0, I thought to get what items that are checked you use the listview.getCheckedItemPositions() but it is not so how do I know what is checked in the list?
this is my listfragment creation
#Override
public void onActivityCreated(Bundle state){
super.onActivityCreated(state);
listview = getListView();
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview.setItemsCanFocus(false);
setEmptyText("No Bowlers");
registerForContextMenu(getListView());
populateList();
}
This post might help. It gives a solution using a custom ResourceCursorAdapter, which provides a CheckBox and a TextView for each ListView row.
To select multiple items in ListView, check this page. Note that the example uses a ListActivity instead of a ListFragment, but your code will end up being extremely similar. Just make sure you implement the Fragment lifecycle methods correctly (i.e. setting the Adapter in the ListFragment's onActivityCreated(), etc.).
I got around the problem by Custom Adapter's bindView, I created an ArrayList<Integer> variable
ArrayList<Integer> mCheckedItems = new ArrayList<Integer>();
and In the bindView I set a checkedchangelistener on the checkbox to see if the box was checked or not. If it was checked I put the id from the database that the cursor got into the mCheckedItems Array
adapter:
public class CheckAdapter extends SimpleCursorAdapter{
Context context;
public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to,int flag) {
super(context, layout, c, from, to);
this.context = context;
}
#Override
public void bindView(View view,Context context,Cursor cursor){
final String name = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));
final int id = cursor.getInt(cursor.getColumnIndex(BowlersDB.ID));
TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
tv.setText(name);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
mCheckedItems.add(id);
}else if(!isChecked){
for(int i=0; i< mCheckedItems.size(); i++){
if(mCheckedItems.get(i) == id){
mCheckedItems.remove(i);
}
}
}
}
});
}
After the id was inserted into the array I used the array list to used them how I needed

Categories

Resources