Remove view from MergeAdapter - android

Is it possible to remove view or adapter from MergeAdapter somehow? I'd try to extend it and remove the view from pieces but it's private. Or maybe there's an alternative solution to show/hide view in this adapter? I tried to set its layout_height to zero and visibility to View.GONE, but it still shows the empty list item. Thanks in advance.

To remove a view or an adapter from MergeAdapter use the following methods:
setActive() on your mergeAdapter Instance.
For Example :
To remove a Textview (mytextView) from a MergeAdapter(merAdapter) use:
merAdapter.setActive(mytextViiew,false);
And to enable it again(to make it visible) use:
merAdapter.setActive(mytextViiew,true);
Refer the MergeAdapter documentation for details:
https://github.com/commonsguy/cwac-merge

Is it possible to remove view or adapter from MergeAdapter somehow?
Not presently, sorry. It shouldn't be too hard to add (remove it from the collection and call notifyDataSetChanged() to update the AdapterView) if you wanted to take a shot at it. Contributions are welcome! :-)

your MergeAdapter should be having method called getCount().. if i have understood you right, returning zero from there may solve your problem..

Expanding on Mark Murphy's answer, I think this is as simple as adding this method to MergeAdapter:
public void removeAdapter(ListAdapter la) {
pieces.remove(la);
}
remove() takes an object and will do all of the necessary testing & removal for you, if that object is contained in the pieces list. You could make this return a bool or something for your own purposes, but I had no such need.
Then just call something like:
int view_to_remove = *AN_INT*
adapter.removeAdapter(listAdapter.getAdapter(view_to_remove));
adapter.notifyDataSetChanged();

Related

Android RecyclerView.Adapter notifyItem drives me crazy

I searched for hours but still have no explanation (even not in official docu) what is the right way to use a RecyclerView.Adapters' notify-methods. Already read a lot of similar question on SO, but no could solve my simple problem. I try to put down a short, simplified example.
class MyAdapterClass extends RecyclerView.Adapter<MyAdapterClass.MyViewHolderClass> {
...
List myItemList = new ArrayList<MyItemType>();
...
}
RecyclerView myRecView = (RecyclerView) findViewById(R.id.myRecView);
myRecView.setAdapter(new MyAdapterClass(...)));
Even when inserting the first item, the view is not refreshed.
myAdapter.myItemList.add(newItem);
myAdapter.notifyItemInserted(myAdapter.myItemList.size() - 1);
If I then tightly touch (try to scroll) the 1-item-listview, the item appears. So it seams to be a refreshing issue. But why? I have an emtpy list, then I insert one item, then I notify the adapter about the insertion. Whats wrong with my expectation?
By the way, even if I additionally call:
myAdapter.notifyItemChanged(myAdapter.myItemList.size() - 1);
it gets not automatically refreshed.
EDIT: And of course, I dont want to use notifyDataSetChanged() :-) only insert, update, remove on an existing list.
Try to add this code inside your adapter class :
#Override
public int getItemCount() {
return myItemList.size();
}
i hope you define in adpter getItemCount() in list size. then used blow code for notify adapter.
myadapter.notifyItemRangeChanged(0, userDataArrayList.size());

How to remove item from bounded List of Views injected by ButterKnife?

I have a List of Views that injected via ButterKnife
#BindViews({
R.id.legend_txt_SB, R.id.legend_txt_MB, R.id.legend_txt_B_LOW,
R.id.legend_txt_B_HIGH, R.id.legend_txt_Normal, R.id.legend_txt_A_LOW,
R.id.legend_txt_A_HIGH, R.id.legend_txt_MA, R.id.legend_txt_SA
})
List<TextView> labels;
And in some case I need to remove 4 and 5 item from this list. I tried next :
if (<expression>) {
labels.get(4).setVisibility(View.GONE);
labels.get(5).setVisibility(View.GONE);
labels.remove(4);
labels.remove(5);
}
and got error :
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
Note. I don't want to disable views. I want to remove them.
I guess you want to remove the view from the screen, not the list.
To do that, you can either hide it using setVisibility or
((ViewGroup) labels.get(4).getParent()).removeView(labels.get(4));
The list contains only pointers to the actual view. I think that you get the exception because "remove" is not implemented in the list ButterKnife returned.
If I where you, I wouldn't use ButterKnife for these particular views, or create a separate field for them in my activity.
I think, ButterKnife annotation does not allow different type than List. Probably, you can try doing:
#BindViews({
R.id.legend_txt_SB, R.id.legend_txt_MB, R.id.legend_txt_B_LOW,
R.id.legend_txt_B_HIGH, R.id.legend_txt_Normal, R.id.legend_txt_A_LOW,
R.id.legend_txt_A_HIGH, R.id.legend_txt_MA, R.id.legend_txt_SA
})
ArrayList<TextView> labels;
UnsupportedOperationException due to AbstractList does not support removing elements, if the List implementation does not have interator that can do removal. Also you can try removing the elements with casting to implementation first:
((ArrayList)labels).remove(4);
((ArrayList)labels).remove(5);

Cannot add header view to list - setAdapter has already been called

FATAL EXCEPTION: main
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
I got this crash, but It didn't always happen! Actually my test phones have no problem. below is my codes.
Constructor
public MyListView(Context context) {
super(context);
adapter = new MytListAdapter(context);
setAdapter(adapter);
}
and I called 'addHeaderView' after getting data from server. so I tried calling addHeaderView before setAdapter and I using visibility of attribute of view. but even if i set the view gone, but it still has a space.
any idea to solve this?
Do not call setAdapter() until after you have called addHeaderView(). In your case, that would mean not calling addHeaderView() or setAdapter() until "after getting data from server".
Or, do not use addHeaderView(), but instead modify the adapter to have an additional row, in the 0th position, after you have retrieved your server data, where the 0th position is your virtual "header".
Actually I solved this problem by using LinearLayout in HeaderView.
I added LinearLayout to HeaderView and if I want to make header invisible, I set GONE to setVisibility method of LinearLayout.
It doesn't make IllegalStateException at all.
Thank you.
To explain in details, as per Android guidelines. When addHeader/addFooter introduced before KITKAT version, developer should call setAdapter only after adding Header/Footer. After release Kitkat means Android Versions>=Kitkat developers can call setAdapter anytime
ref : https://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View,%20java.lang.Object,%20boolean)

Android custom list view with custom adapter not clear

I want a custom list view with a custom array adapter.I found this blog post but customadapter.java is unclear. I can do the about with default simple_list_1.
Here is the blog post http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/
How is constructor getting called.How is static class called.Totally,I'm not able to under stand the flow of the program.
Thank you.
getview() method will be called automatocally for every list you will add.
Take a look here for more explanation on array adapter.
Hope it will help.

How to refresh dynamic listview?

I have a dynamic listview and I want to refresh the list when any new item is added.
How can I do this.Pls help me.
Thanks,
Monali
Call notifyDataSetChanged() on your adapter
Here is a demo for how u can do that http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
USE setListAdapter(yourAdapter);
Make your adapter to ListAdapter find more details here
then just use
yourAdapter.notifyDataSetChanged(); and you are done !
1) create a method
2) set adapterView in that method.
3) at click event of any image button call that method.
or to put Adapter.notifyDataSetChanged();
I think this would be easy for you.
Also you can do this like...
I have custom row for list. It has textview and I can change it's value ...
((TextView)(myWatchList.getChildAt(INDEX - myWatchList.getFirstVisiblePosition()).findViewById(R.id.symbolCountTV))).setText(NEWSTRING);

Categories

Resources