Reputation: 1469
I have a special question which i havent't found the answer. I can't deal with this problem.
So... is there any way to hide or show items(images) dynamically on ListView
?
I mean, after tap on toolbar menu item called "Edit", on ListView
next to the texts should appear images. When user click on this image, this text should be removed from ListView
and database. And, after tap on "Done", images that have previously appeared should gone.
I spent all night for this and I haven't found the answer, so guys please help me!
I have this:
And want this after click on Edit:
That's part of my Adapter (extends from BaseAdapter)
public View getView(final int position, View convertView, final ViewGroup parent) {
if(convertView == null){
convertView = inflater.inflate(R.layout.list, null);
TextView textView = (TextView) convertView.findViewById(R.id.textView);
textView.setText(todo.get(position));
ImageView circleImage = (ImageView) convertView.findViewById(R.id.circleImage);
Picasso.with(mActivity).load(R.drawable.circle_image).transform(new CircleTransform()).into(circleImage);
}
if(editMode){
circleImage.setVisibility(View.GONE);
} else circleImage.setVisibility(View.VISIBLE);
return convertView;
}
public void setMode(boolean editMode){
this.editMode=editMode;
notifyDataSetChanged();
}
onOptionItemSelected from MainActivity:
else if(id == R.id.edit){
mToDoFragment = new ToDOFragment();
mFToDoFragment.setEditable(true);
invalidateOptionsMenu();
return true;
}
And, last part from ToDoFragment:
public void setEditable(boolean editable){
mFavouritesListAdapter.setMode(editable);
}
After that, I'm getting such beutiful NullPointerException :(
java.lang.NullPointerException: Attempt to invoke virtual method 'void adapters.ToDoListAdapter.setMode(boolean)' on a null object reference
Upvotes: 0
Views: 89
Reputation: 1971
Hi please follow the blow steps. I hope it will help you.
First you have flag that indicate that current mode for example the list view in edit mode or in normal mode (view mode)
After define the mode. Take the array list with your model or object. bind you array list with BaseAdapter
. I guess you are using the ListView
. If you are using the RecyclerView
then the process or logic will remain the same.
In inside the getView(....)
. Define you layout using LayoutInflater
After checking the convert-view is not null and inflating the layout.
Put the logic for check the flag of mode.
Check whether the mode is edit mode or normal view mode. If edit then show the [x] icon for each row item otherwise hide/gone the image view.
If the mode is edit when click on [x] image view remove position of the model or object from the dataset that is bind with the BaseAdapter
. And call notifyDatasetChanged()
. This will refresh the list view and remove the item from listview.
If edit mode is complete and user click on "Done" Option from menu item. Then simply change the flag of mode from edit to done mode. call again notifyDatasetChanged()
method of your BaseAdapter
That's it. You done let me know if you have any query.
Upvotes: 2