I've got a Lsitview populated with CheckedTextViews. My goal is to be able to delete multiple rows at onece, if possible with MultiChoiceModeListener.
To use it, I have to set the ListView CHOICE_MODE_MULTIPLE_MODAL.
The frist problem is now: I cant just chek the checkboxes now, because the listview isnt ChOICE_MODE_MULTIPLE anymore.
I hade the idear to set it CHOICE MODE MULTIPLE and just set an OnItemClickListenerfor the ListView, but then the ChoiceModeMultiple and its Method onItemStateChaed Method always gets called, even if i just wanna check a checkbox in my App.
Any solutions on how to fix that?
The Code (layout is the ListVIew) :
layout.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(android.view.ActionMode actionMode, int i, long l, boolean b) {
if (!list_items.contains(list.get(i))) {
count++;
list_items.add(list.get(i));
actionMode.setTitle(count + " items selected");
} else if (list_items.contains(list.get(i))) {
count--;
list_items.remove(list.get(i));
actionMode.setTitle(count + " items selected");
}
}
}
Try this example, please refer this and solve
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> arrayList = new ArrayList<>();
ArrayList<String> arrayList2 = new ArrayList<>();
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listItems);
arrayList.add("one");
arrayList.add("two");
arrayList.add("three");
arrayList.add("four");
arrayList.add("five");
arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_layout,R.id.textView,arrayList);
list.setAdapter(arrayAdapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {
count = count+1;
actionMode.setTitle(count + " items selected");
arrayList2.add(arrayList.get(position));
}
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.list_menu,menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.delete_id:
for(String msg : arrayList2) {
arrayAdapter.remove(msg);
}
Toast.makeText(getApplicationContext(),"deleted",Toast.LENGTH_SHORT).show();
count=0;
actionMode.finish();
return true;
default:
Toast.makeText(getApplicationContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapterView.setSelected(true);
}
});
}
}
Related
I want to add a counter instead of a normal title when items get selected, for example, if the user select 2 items the title of the cab should change to 2.
i have tried setting actionMode.setTitle(String.valueOf(counter)),whre selected_items_list is where all selected items get added , but it just set to title to 0 inside onPrepareActionMode.
The Counter is increased when the Update inside TextViewHolder method is called (inside of the onClick listeners).
I'm using a RecyclerView, below is the ViewHolder and the ActionMode.Callback.
the CAB contains only two elements/items: Delete and share buttons.
EDIT
Moved actionMode.setTitle() inside Update method (TextViewholder) .
title is changed to the number of elements selected. now finding how to decreased when items get deselected.
ViewHolder
public class TextViewHolder extends ViewHolder {
// each data item is just a string in this case
private TextView titolo;
private TextView ora;
private RelativeLayout selezione_layout_testo;
public TextViewHolder(View v) {
super(v);
titolo = itemView.findViewById(R.id.testo);
selezione_layout_testo =
itemView.findViewById(R.id.layout_selezione_testo); //This element
is root view of the items
ora = itemView.findViewById(R.id.ora);
}
void selectItem(ModelloDati item) {
if (multiSelect) {
if (selected_items_list.contains(item)) {
selected_items_list.remove(item);
if(selected_items_list.size()==0)
{
if (actionMode != null ){
actionMode.finish();
}
}
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.transparent));
} else {
selected_items_list.add(item);
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.Colore_selezione));
}
}
}
void update(final ModelloDati value) {
if (selected_items_list.contains(value)) {
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.Colore_selezione));
} else {
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.transparent));
}
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
actionMode =
((AppCompatActivity)view.getContext()).startSupportActionMode(
actionModeCallback);
selectItem(value);
//-------------- EDITED!!!
String counter_string = String.valueOf(counter);
actionMode.setTitle(counter_string);
return true;
}
});
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
counter++;
selectItem(value);
}
});
}
}
ActionMode.Callback
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(final ActionMode actionMode, Menu menu)
{
multiSelect = true;
actionMode.getMenuInflater().inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(
ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.delete:
for(ModelloDati oggetto : selected_items_list)
{
if(oggetto.getTYPE() == tlTEXT)
data_list.remove(data_list.indexOf(oggetto)-1);
data_list.remove(oggetto);
}
actionMode.finish();
return true;
case R.id.share:
//Do nothing
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
multiSelect = false;
selected_items_list.clear();
notifyDataSetChanged();
counter = 0;
}
};
Variables
private ArrayList<ModelloDati> data_list;
private ArrayList<ModelloDati> selected_items_list = new ArrayList<>();
private Context context;
private static final int tlTEXT = 0;
private boolean multiSelect = false;
private ActionMode actionMode;
private int counter = 0;
I have seen a lot of tutorials where the counter is used but I'm unable to implement it inside my code :(
This is basically a notes app,in which we dynamically add data to our app by taking in title and description from the user,the problem is that when we search about some note by its title then instead of giving the possible notes the data set in the adapter vanishes,the logic is written in my filter() function in the adapter class
MainActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private ArrayList<Notes> list;
private NotesAdapter notesAdapter;//this is our notes adapter
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
#Override //
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list=new ArrayList<>();//this is the list which we have to pass into adapter
recyclerView=findViewById(R.id.rv);
notesAdapter=new NotesAdapter(this,list);
View dialogView= LayoutInflater.from(this).inflate(R.layout.dialog_main,null,false);
final EditText title=dialogView.findViewById(R.id.t);
final EditText description=dialogView.findViewById(R.id.d);
final AlertDialog alertDialog=new AlertDialog.Builder(this)
.setTitle("Enter the details:")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
list.add(new Notes(title.getText().toString(),description.getText().toString(),false));
notesAdapter.notifyItemChanged(list.size());
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).create();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override // on pressing the fab we will get an alert dialog box where we can add title and description and with the option to add it or not
public void onClick(View view) {
alertDialog.show();
}
});
recyclerView.setAdapter(notesAdapter);
linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
}
#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);
final MenuItem searchItem = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}``
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.search) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onQueryTextSubmit(String s) {
notesAdapter.filter(s);
return true;
}
#Override
public boolean onQueryTextChange(String s) {
notesAdapter.filter(s);
return true;
}
NotesAdapter.java
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NotesHolder> {
private ArrayList<Notes> arrayList;
private ArrayList<Notes> arrayListCopy;
Context c;
NotesAdapter(Context context,ArrayList<Notes> list){
this.arrayList=list;
this.c=context;
this.arrayListCopy=new ArrayList<>(list);//this is where I store identical list which I get from Adapter
}
public class NotesHolder extends RecyclerView.ViewHolder {
TextView textView;
public NotesHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv);
}
}
#Override
public NotesAdapter.NotesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NotesHolder(LayoutInflater.from(c).inflate(R.layout.item_row,parent,false));
}
#Override
public void onBindViewHolder(final NotesAdapter.NotesHolder holder, final int position) {
final Notes currentNote=arrayList.get(position);
holder.textView.setText(currentNote.getTitle());
holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
arrayList.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
return true;
}
});
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(c,AnotherActivity.class);
intent.putExtra("NAME",currentNote.getDescription());
c.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public void filter(String text){//This is my filter function
arrayList.clear();
if(TextUtils.isEmpty(text)){
arrayList.addAll(arrayListCopy);
}
else{
text=text.toLowerCase();
for(Notes note:arrayListCopy){
if(note.getTitle().toLowerCase().contains(text)){
arrayList.add(note);
}
}
}
notifyDataSetChanged();//the data set is still not updated and instead it vanishes
}
}
As soon as I search something the whole list gets vanished ,where am I missing out on?How should I modify my filter function in adapter class?
Change your getItemCount :
#Override
public int getItemCount() {
return arrayListCopy.size();
}
Try this:
NotesAdapter(Context context,ArrayList<Notes> list){
this.arrayList = new ArrayList<>(list.size());
this.c=context;
this.arrayListCopy=new ArrayList<>(list);//this is where I store identical list which I get from Adapter
}
#Override
public int getItemCount() {
return arrayList.size();
}
public void filter(String text){//This is my filter function
arrayList.clear();
if(text.trim().isEmpty() || text == null){
arrayList.addAll(arrayListCopy);
}
else{
text=text.toLowerCase();
for(Notes note:arrayListCopy){
if(note.getTitle().toLowerCase().contains(text)){
arrayList.add(note);
}
}
}
notifyDataSetChanged();//the data set is still not updated and instead it vanishes
}
Original Answer:
Here you are referencing the same variable (i.e. arrayList and arrayListCopy ) are pointing to the same variable. Instead, initialize arrayList like this inside your constructor:
this.arrayList = new ArrayList<>(arrayListCopy.size());
The problem is that arrayList and arrayListCopy are references to the same list.
NotesAdapter(Context context,ArrayList<Notes> list){
this.arrayList=list;
this.c=context;
this.arrayListCopy=list;
}
Changes done to one of them, will be reflected in both. For example arrayList.clear() will also empty the arrayListCopy list.
What you could do is something like this:
List<Notes> originalList;
NotesAdapter(Context context,ArrayList<Notes> list){
this.arrayList=list;
this.c=context;
this.originalList= new ArrayList<>(list); // create a new List that contains all the elements of `list`.
}
And to filter, do this:
public void filter(String text){
arrayList.clear();
if(text.isEmpty()){
arrayList.addAll(originalList);
} else{
text=text.toLowerCase();
for(Notes note : originalList){
if(note.getTitle().toLowerCase().contains(text)){
arrayList.add(note);
}
}
}
notifyDataSetChanged();
}
It seems that you're adding items to the list inside the MainActivity after you create the adapter which means that you'll have to use some other mechanism for adding new items so that originalList will contain all the items. Something like:
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NotesHolder> {
public void addItem(Notes item){
originalList.add(item);
if(text.isEmpty() || item.getTitle().toLowerCase().contains(text.toLowerCase())){
arrayList.add(item);
}
}
}
Here, text is just a reference to the text variable passed to the filter method.
From MainActivity when a new item is created, do this:
adapter.addItem(new Notes(title.getText().toString(),description.getText().toString(),false));
I am using a contextual action bar and wish to retrieve each value selected to pass to another activity after I click on the 'mail' button. How can I do this?e
Code for the CAB.
mAdapter = new SelectionAdapter(this, R.layout.activity_result, R.id.name, new String[] {TAG_NAME, TAG_ROOM_PRICE});
setListAdapter(mAdapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.email:
}
return false;
}
#Override
public void onDestroyActionMode(android.view.ActionMode mode) {
mAdapter.clearSelection();
}
#Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle("No: of resorts selected: " + nr);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return true;
}
});
Selection adapter class which is used to define the object mAdapter
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
I wish to retrieve the String value of every selection that I make
I found the solution.
mAdapter.setNewSelection(position, checked);
String get_list = resortsList.get(position).get(TAG_NAME);
stringList.add(get_list);
And to remove the item selected when deselected:`int i;
for(i = 0 ; i < stringList.size(); i++){
if(stringList.get(i).equals(resortsList.get(position).get(TAG_NAME))){
stringList.remove(i);
Log.d("String List: ", stringList.toString());
break;
}
}`
This question already has answers here:
how to attach multiple files to email client in android
(5 answers)
Closed 3 years ago.
I have done coding for selected multiple listview items to be shared but the problem is when i select the multiple items it shares only one item, the remaining items are not selected.Please help me how to share the multiple listview items .
public class MainActivity extends AppCompatActivity {
TextView textView;
ListView listView;
List<String> myList;
String MEDIA_PATHS;
Uri uri;
int count = 0;
ArrayList<String> arrayList2 = new ArrayList<>();
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/CallLogs" );
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.list_items);
textView = (TextView)findViewById(R.id.textView);
myList = new ArrayList<String>();
final File files = new File(MEDIA_PATH);
File list[] = files.listFiles();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
final ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_layout,R.id.textView,myList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {
count = count+1;
actionMode.setTitle(count + " items selected");
MEDIA_PATHS = new String(Environment.getExternalStorageDirectory() + "/CallLogs/" + myList.get(position));
// arrayList2.add(myList.get(position));
arrayList2.add(MEDIA_PATH);
}
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.list_menu,menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.delete_id:
for(String msg : arrayList2) {
adapter.remove(msg);
}
Toast.makeText(getApplicationContext(),"deleted",Toast.LENGTH_SHORT).show();
count=0;
actionMode.finish();
return true;
// break;
case R.id.share_id:
for(String msg : arrayList2) {
uri = Uri.parse(msg);
}
// Uri uri = Uri.parse(MEDIA_PATHS);
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share audio File"));
return true;
default:
Toast.makeText(getApplicationContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// myList.get(position);
adapterView.setSelected(true);
}
});
}
}
Please refer this link for more information about sharing the files.I tried it,works for me.
https://developer.android.com/training/sharing/send.html
I have set a ListView with a custom CursorAdapter in MainActivity, and when "EDIT" menu is clicked, the CursorAdapter changes its View to set visibility of CheckBoxes.
At this, I found that the onClickListener is aware of changes of the CheckBox not in the MainAcitivity but in the CursorAdapter class. So I coded that the id values are restored in a ArrayList whenever the CheckBoxes are clicked, and returned when a method is called in MainActivity.
The problem is, I want for option menus to be changed if the CheckBox is checked (for example, "DELETE" and "MOVE" menu is unable when no CheckBox is checked, and enabled once CheckBoxes are clicked), but don't know how.
Some parts of my MainActivity and CursorAdapter
MainActivity.java:
public class MainActivity extends Activity {
private MenuItem itEdit, itSort, itView, itSelectAll, itDeselectAll, itDelete, itMove;
private boolean isEditChecked;
private ListView lvWords;
private WordAdapter wordadapter;
private ArrayList<Integer> checkedWords;
private Cursor wordCursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvWords = (ListView) findViewById(R.id.lv_words);
dbhandler = DBHandler.open(this);
wordCursor = dbhandler.selectWords(activeNoteId);
wordadapter = new WordAdapter(this, wordCursor, isEditChecked);
lvWords.setAdapter(wordadapter);
....
...
..
}
protected void onResume() {
super.onResume();
refreshListViews();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
itEdit = menu.findItem(R.id.it_edit);
itSort = menu.findItem(R.id.it_sort);
itView = menu.findItem(R.id.it_view);
itSelectAll = menu.findItem(R.id.it_select_all);
itDeselectAll = menu.findItem(R.id.it_deselect_all);
itDelete = menu.findItem(R.id.it_delete);
itMove = menu.findItem(R.id.it_move);
itEdit.setVisible(true);
itSort.setVisible(true);
itView.setVisible(true);
itSelectAll.setVisible(false);
itDeselectAll.setVisible(false);
itDelete.setVisible(false);
itMove.setVisible(false);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
....
...
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.it_edit:
onEditMenuClicked();
break;
case R.id.it_delete:
checkedWords = wordadapter.getCheckedArrayList();
final int checkedDeleteSize = checkedWords.size();
....
...
break;
}
return super.onOptionsItemSelected(item);
}
// when edit menu clicked
private void onEditMenuClicked() {
isEditChecked = !isEditChecked;
itSort.setVisible(!isEditChecked);
itView.setVisible(!isEditChecked);
itSelectAll.setVisible(isEditChecked);
itDeselectAll.setVisible(isEditChecked);
itDelete.setVisible(isEditChecked);
itMove.setVisible(isEditChecked);
wordadapter.setVisibility(isEditChecked);
refreshListViews();
}
private void refreshListViews() {
wordCursor.close();
wordCursor = dbhandler.selectWords(activeNoteId);
wordadapter.changeCursor(wordCursor);
setVisibilityViews();
}
}
WordAdapter.java:
public class WordAdapter extends CursorAdapter {
private boolean isCheckBoxVisable;
private ArrayList<Integer> checkedArray;
private boolean setCheck;
public WordAdapter(Context context, Cursor c, boolean check) {
super(context, c);
isCheckBoxVisable = check;
checkedArray = new ArrayList<Integer>();
setCheck = false;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
CheckBox check = (CheckBox) view.findViewById(R.id.cb_list_word_check);
final int id = cursor.getInt(cursor.getColumnIndex("_id"));
if(isCheckBoxVisable)
check.setVisibility(View.VISIBLE);
else check.setVisibility(View.GONE);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/////// I think something is added here /////
if(isChecked){
checkedArray.add(id);
}
else{
checkedArray.remove(new Integer(id));
}
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d("newView()","start of newView");
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.word_list_row, parent, false);
Log.d("newView()","end of newView");
return v;
}
// set visibility of checkbox
public void setVisibility(boolean check){
isCheckBoxVisable = check;
}
public ArrayList<Integer> getCheckedArrayList(){
return checkedArray;
}
}
If you want to perform a menu update, you must call invalidateOptionsMenu(). It will call onCreateOptionsMenu(Menu menu) back, were you can update (re-create) a menu depending on the state your activity is in.
Runtime changes in menu are explained here in more details.