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
Related
I am having some serious issues with this, I have a listview that is populated with entries in a database, I have a custom row for each of the row in the listview (TextView1|TextView2|TextView3|Checkbox).
All I want to do is put a listener on each of the check boxes so when it is checked it is removed from the listview and deleted from the database. I have a function that deletes the row from the database when it is passed the value for Textview1.
The issue I am having is trying to get the row id from the checked box or even the textview value. I have searched everywhere but still cant get anything to work
CheckBox
Cursor cursor = db.getAllItems();
//String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
final String[] columns = new String[] {db.KEY_ITEM_NAME, db.KEY_MEASUREMENT, db.KEY_UNIT};
int[] to = new int[] {R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row4, cursor, columns, to, 0);
final ListView shoppingList = (ListView) findViewById(R.id.shoppingList);
shoppingList.setClickable(false);
//shoppingList.setChoiceMode(shoppingList.CHOICE_MODE_SINGLE);
shoppingList.setAdapter(myCursorAdapter);
CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
//how do i get the row_id or the text view value?
} else {
//do nothing
}
}
});
As far as I know it is not possible with this implementation. You have to create a custom adapter
public class CustomAdapter extends SimpleCursorAdapter{
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
public View getView(final int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
// position will give you the position of the clicked element from where you can fetch your data
} else {
//do nothing
}
}
});
return view;
}
}
you can use it by
CustomAdapter adapter = new CustomAdapter(this, R.layout.row_layout, cursor, from, to);
list.setAdapter(adapter);
Use getTag() method.
For example, check.setTag( your wanted id) `
In listerner
int id =(Integer) Buttonview.getTag()
So I have a listview that I want to add checkboxes to.
lv = (ListView)findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listItems);
lv.setAdapter(adapter);
lv.setItemsCanFocus(true);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
This works and the checkboxes show up. Then I have my setOnItemClickListener() for my listview because the user needs to select an item, then the next acitivty will be launched
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent components = new Intent();
components.setClass(context, ComponentsActivity.class);
components.putExtra("studyID", studyID);
components.putExtra("studyName", studyName);
startActivity(components);
}
});
However, I want to add a checkbox so that the user can tick that item in the listview to perform other actions. The problem is I can't differentiate the events. When I click on the checkbox, it gets checked but then the list item also gets selected and the new activity starts. I only want the checkbox to be affected when they click on it, not have it launch the new acitivty. I know you can also just create your own adapter but why bother if I can make a checkbox in 2 lines of code. Any suggestions? I just want to be able to check the textbox and get the id of the checked items.
I never managed to find anything for what I was looking for so I bit the bullet and decided to learn how to make my own custom adapter class. Here is my code if anybody ever runs into this problem. This adapter class is for a listview with text(TextView) and a checkbox.
public class CustomAdapter extends BaseAdapter
{
ArrayList<String> studies;
Context context;
LayoutInflater myInflater;
ArrayList<Boolean> positionArray;
public CustomAdapter(ArrayList<String> arr, Context c)
{
studies = arr;
context = c;
myInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
positionArray = new ArrayList<Boolean>();
for(int i = 0; i < studies.size(); ++i)
{
positionArray.add(false);
}
}
#Override
public int getCount() {
return studies.size();
}
#Override
public Object getItem(int i) {
return studies.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
public void remove(int i)
{
this.studies.remove(i);
this.positionArray.remove(i);
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
final int pos = position;
Holder holder = null;
//Create the views and populate it with an element from teh array
if(view == null)
view = myInflater.inflate(R.layout.custom_list_layout, viewGroup, false);//made my own layout for each listview 'cell'
holder = new Holder();
TextView study = (TextView)view.findViewById(R.id.adapterTextView);
holder.ckbox = (CheckBox)view.findViewById(R.id.adapterCheckBox);
holder.ckbox.setOnCheckedChangeListener(null);
study.setText(studies.get(position));
holder.ckbox.setFocusable(false);
//Since this method gets called whenever we scroll(view recycling), we have to re-check the checkboxes
holder.ckbox.setChecked(positionArray.get(position));
holder.ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//checkBoxArray[pos].setChecked(isChecked);
positionArray.set(pos, isChecked);
}
});
return view;
}
static class Holder
{
CheckBox ckbox;
}
}
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
My activity contains a button and a ListView. The ListView contains a Spinner and an EditText view. I use the button each time I want to insert a new row entry in my Activity's ListView.
I have followed the instructions of previous stackoverflow threads like this one here: Android Listview with spinner and a checkbox on how to populate ListViews with focusable objects like Spinners.
My problem is that each time I dynamically add a new ListView entry in the ListView, the Spinner value of the previous ListView entry is lost (actuall the Spinner returns to its default setting). Say for simplicity that my Spinners are populated with the following data:
String spinner_data[] = {"apple", "banana", "pear", "watermelon", "strawberry"};
For example, if I select my first ListView's Spinner value to be "pear" and then I add a new ListView entry with my Button, the "pear" entry disappears from the 1st ListView Spinner and the default value "apple" appears).
Any help is appreciated!
This is my activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
da = new DataAdapter(this, new ArrayList<RawData>());
lv.setAdapter(da);
btn_new = (Button)findViewById(R.id.btn_new);
btn_new.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
da.add(new RawData(this));
da.notifyDataSetChanged();
}
});
}
The RawData class is this one:
public class RawData {
private int selected_position;
private ArrayAdapter<CharSequence> adapter;
public RawData(Context context)
{
adapter = ArrayAdapter.createFromResource(context, R.array.data, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter()
{
return adapter;
}
/**
* get adapter's item text from selected position
* #return
*/
public String getText()
{
return (String) adapter.getItem(selected_position);
}
public int getSelected()
{
return selected_position;
}
public void setSelected(int selected)
{
this.selected_position = selected;
}
}
The DataArrayAdapter is the following:
public class DataArrayAdapter extends ArrayAdapter<RawData> {
private Activity myContext;
//private final List<RawData> list;
public DataArrayAdapter(Activity context, List<RawData> list)
{
super(context, R.layout.row_view, list);
myContext = context;
}
static class ViewHolder
{
protected RawData data;
protected Spinner spn;
protected EditText edt;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if ( convertView == null )
{
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.row_view, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.edt = (EditText)view.findViewById(R.id.edt);
viewHolder.data = new RawData(myContext);
viewHolder.spn = (Spinner)view.findViewById(R.id.spn);
viewHolder.spn.setAdapter(viewHolder.data.getAdapter());
viewHolder.spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2_position, long arg3) {
// TODO Auto-generated method stub
viewHolder.data.setSelected(arg2_position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// Update the TextView to reflect what's in the Spinner
view.setTag(viewHolder);
}
else
{
view = convertView;
}
// This is what gets called every time the ListView refreshes
ViewHolder holder = (ViewHolder) view.getTag();
holder.spn.setSelection(getItem(position).getSelected());
return view;
}
}
You're not handling the situation when getView gets a non-null convertView. In your example, after you add an item, ListView refreshes itself, and position that should display 'pear' gets an existing convertView (the one that was used previously to display 'apple') and you just pass it along to ListView without setting the data for current position. You cannot rely on ListView items to store any data, you should always set correct contents for position in getView method of your adapter.
Just to be clear, I see that your code sets the selected position in the end of getView but the issue is that whatever is tagged to your convertView when it is passed to getView by recycler mechanism in ListView is random and can be associated with any position it used to display before.
To make your application work you'll have to create array of selectedItem values for all your spinners, and attach it as a member to your adapter. You'll have to update the corresponding value on each OnItemSelected event and you'll add a new value for each "add" button click. And when you prepare your view in getView you'll just set the selected spinners index to corresponding value in your array.
I've created a custom ListView with checkboxes. I wanted to delete the selected items on "Delete" menu option selection.
I am handling the onCheckedChanged event on CheckBox. Here I maintain an ArrayList to note down the selected elements position. Now when the user selects the "Delete" menu item I do remove the choosen items from ListAdapter's ArrayList.
Below is my code,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
names = new ArrayList<String>();
names.add("first");
names.add("second");
names.add("three");
names.add("four");
names.add("five");
names.add("six");
names.add("seven");
names.add("eight");
names.add("nine");
listAdapter = new SelfAdapter(this,names);
this.setListAdapter(listAdapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem){
int menuId = menuItem.getItemId();
ArrayList<Integer> selectedItems;
switch(menuId){
case R.id.delitem:
Log.d(TAG,"del item selected");
selectedItems = listAdapter.getSelected();
**//pick the selected position items and delete them from the
// listadapter arraylist**
for(Integer element: selectedItems){
Log.d(TAG,"Selected:"+element.toString());
names.remove(element.intValue());
listAdapter.notifyDataSetChanged();
}
}
return true;
}
Here is the code for custom adapter,
class SelfAdapter extends ArrayAdapter<String>{
ViewHolder holder = new ViewHolder();
private Activity context;
private ArrayList<String> names;
private String TAG = "SelfAdapter";
ArrayList<Integer> checkedPos; **//contains the selected items position**
public SelfAdapter(Context context, ArrayList names) {
super(context,R.layout.checkboxnlist, names);
this.context = (Activity) context;
this.names = names;
checkedPos = new ArrayList<Integer>();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
final int listPosition = position;
if(convertView == null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.checkboxnlist,parent,false);
holder.chkBox = (CheckBox)convertView.findViewById(R.id.chkvw);
holder.txtView = (TextView)convertView.findViewById(R.id.txtvw);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtView.setText(names.get(position));
holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG,"listPosition:"+listPosition);
if(isChecked){
checkedPos.add(new Integer(listPosition));
}
else{
checkedPos.remove(listPosition);
}
}
});
return convertView;
}
public ArrayList<Integer> getSelected(){
return checkedPos;
}
static class ViewHolder{
TextView txtView;
CheckBox chkBox;
}
}
Now the problem is that if select 1st,2nd items (for example) then I see the wrong items get deleted and the listview populate after this is completely wrong. Later if I attempt to delete the items it's throwing "Index out of bounds exception". I guess the code is going wrong in getView method.
Can some one help me in finding out what am doing wrong here.
Thanks
When you remove elements from names, you also need to remove elements from the checkedPos array. Otherwise, the two arrays will have different lengths. Then when you try again, checkedPos will be longer than names and you end up with the index out of bounds exception.
Also, when you are deleting from names, you can't use a normal iterator through . If you are supposed to delete names at indexes 1 and 2, then when you delete name 1, name 2 moves up to position 1. But your indexing isn't updated accordingly. The easiest solution is to iterate backwards through the position array, deleting from the end of names before deleting from the beginning.
P.S. It's a little weird to switch from ordinal numbers ("first", "second") to cardinal numbers ("three", "four", etc.) in the middle of the list.
you must delete
names = new ArrayList< STRING >();
but you delete selectedItems;