Deleting Custom List view item android without remove method - android

I have created a custom list view whose parent class is Base Adapter. Now i need to delete its menu item .i searched for it and most of the tutorial says to use "remove" method and will be done.But actually in all tutorials they extend their class from Array-adapter.Now problem in my case is i didn't find the remove method in Base Adapter class to use it . so how i can remove my list view item in this case ?.

You won't find a remove in a BaseAdapter, because ArrayAdapter is already an Array so..
What you can do is remove the item from the ArrayList<> and then set it again for the adapter, and after that call notifyDataSetChanged(),
have a setData(ArrayList) in your adapter so you don't have to instantiate a new one. i.e.
setData(ArrayList list) {
this.list = list;
notifyDataSetChanged();
}
hope this helps

Related

How to show All list items in child ListView-using cwac-merge-1.0.4 jar

I am using two ListView in same screen,Because of using two Listviews likely Parent ListView and Child ListView,that Child ListView Adapter class doesn't show all List Values in Child ListView.It showed only 0 th(first) position in Child ListView,for this above issue I have used the answer code from this Android list view inside a scroll view link.now I can see all list values in my Child ListView but It is not showing all my List values in same time which I had grabbed from Web service call.It showed first value only in list because the List View height was set to list's single item height.If I scroll down the Child ListView I am able to see all values in ListView.What I need is If i get five list values from Web service It has to show all five items in same time.
note: If I set hard coded values for List,It showed all items in Child ListView at one time.
Edited
How I achieved it using MergeAdapter
This createAdapter method would return Adapter object and I have set that Adapter into my Listview using setAdapter(createAdapter(new ArrayList<T>()))
private ListAdapter createAdapter(List<T> items) {
mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(endlessFeedAdapter);
return mergeAdapter;
}
What I need is
I Have been suggested to use ExpandableListView instead of using two ListViews.
If I use ExpandableListView I will have to change the createAdapter method return type into ExpandableListAdapter for this I used below code
private ExpandableListAdapter createAdapter(List<T> items) {
mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(endlessFeedAdapter);
return (ExpandableListAdapter) mergeAdapter;
}
but it showed the below Excaption
Caused by: java.lang.ClassCastException: com.commonsware.cwac.merge.MergeAdapter cannot be cast to android.widget.ExpandableListAdapter
Values from Web service
Hard coded values
What is stopping you to make 2 different adapters and adding them to MergeAdapter? You can add multiple adapters to MergeAdapter and multiple views. In that case there is no need to use 2 Listviews.
mergeAdapter.addAdapter(adapterHeading);
mergeAdapter.addView(someView);
mergeAdapter.addAdapter(adapterFooter);
listView.setAdapter(mergeAdapter);

Find Listview clicked section adapter in mergeadapter?

I am using merge adapter to merge two custom cursor adapters in android.I could not find clicked section to get data from my custom cursor adapter.How can I get adapter object when listview clicked?.
I tried following way in my onitemclick of listview. But it does not print "clicked on mention question section" text.But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638.How can I find which section is clicked in listview?
if (parent.getAdapter().getItem(position) instanceof FeedMentionQuestionAdapter) {
LivLog.info(getClass(), "clicked on mention question section ");
}
How can I get adapter object when listview clicked?
You need to hold onto your own adapter, or call getAdapter() on your ListView.
But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638
Presumably, you put a CursorAdapter in the MergeAdapter.
Also, getItem() will never return an adapter. It returns items. If you are trying to determine the adapter that handles a particular position, call getAdapter().
How can I find which section is clicked in listview?
If you are referring to SectionIndexer, then sections are not clicked.

Getting all of the items from an ArrayAdapter

I have a ListFragment backed by an ArrayAdapter that gets populated by a Loader. When the user clicks on one of the items, I want to pass a reference to the selected item, as well as the rest of the list items to another fragment. My question is how should I get all of the items from the adapter? Here are the possibilities that I see:
1. Keep a reference to the backing List
Create the adapter like so:
List<DomainObject> items = new ArrayList<DomainObject>();
listAdapter = new ArrayAdapter<DomainObject>(getActivity(), R.layout.mine, items);
and then simply pass items or a copy of it to the next activity.
The downside I see of this is that I'm relying on the undocumented fact that the same list that I pass to the constructor contains the items later on.
2. Iterate through the adapter
When an item is clicked, iterate through the adapter and build up the list. This seems like an unnecessary amount of work. The items are contained in a List in the adapter and I'm manually copying each item to a new list.
3. Keep a separate list of items when adding to adapter
Before adding an item to the adapter, add it to a separate list that I maintain in the fragment. This is also wasteful as the list of items is copied in the ArrayAdapter and the fragment.
I'm a little late to the game, but I've run up against a similar issue.
One way to deal with #1 would be to maintain the reference to the list within a subclass of ArrayAdapter, so that your reuse is controlled by the adapter object.
Something like:
public class DomainAdapter extends ArrayAdapter<DomainObject> {
private final List<DomainObject> items;
public DomainAdapter(Context context, List<DomainObject> items) {
super(context, R.layout.mine, items);
this.items = items;
}
public List<DomainObject> getItems() {
return items;
}
}
The solution that I've gone with in the meantime is just to not use ArrayAdapter. In cases where you're fighting against this API, it seems like it's better just to use the less fully-featured (and complex) BaseAdapter. You can read more about the decision to go with BaseAdapter instead of ArrayAdapter in this article: Android Adapter Good Practices.
A quick test says that method 1 works. It seems the quickest and cleanest, but since it is undocumented you may want to test it across the intended platforms and whenever they update in case the underlying structure of ArrayAdapter changes.
I am using compile SDK version 22 and min SDK Version 10.
The best method is to "keep a reference to the List" BUT not passing "items" variable/parameter to the Constructor:
List<DomainObject> items = new ArrayList<DomainObject>();
listAdapter = new ArrayAdapter<DomainObject>(getActivity(), R.layout.mine);
In this way you only instantiate the ArrayList as an empty array and you will have to manage YOUR list by yourself.
I think first method is best way to do this.
I dont think, Data would be original for the Another Activity. because, You would pass items through bundle, so the object is written on bundle first and then in next Activity we read from bundle.
However, if you are using some other way to pass the list, use list.clone() to create new Object, instead of passing original one.

How to get the ListAdapter and update the shown list on an Android Activity Screen

I am setting up a view of a list. I got the list from a remote server call in a AsynchTask call and now in the onPostExecute method I am trying to update the list view with the items I got from the db.
I have some code like this:
ListAdapter l = getListAdapter();
But the ListAdapter does not have the notifyDataSetChanged(); that I need to call to change the original list that was set.
How do I update my list that is shown to the screen?
Thanks!
Depending on which adapter you actually used and which class it extends you may coerce your adapter like this:
((ArrayAdapter<MyClass>) yourListView
.getAdapter()).notifyDataSetChanged();
This is required since only certain adapters have this method.
While I have defined my own adapter like this:
public class MyAdapter extends
ArrayAdapter<MyClass> {...

Which is the correct way to add item into ArrayAdapter

Consider the following code.
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, resource, textViewResourceId, list);
// Method 1 : Add an item.
adapter.add("ITEM1");
// Method 2 : Add an item
list.add("ITEM2");
I was wondering, which is the correct way to add item into ArrayAdapter? As seems to me, both methods just work fine.
Method 1 updates the associated AdapterView, if you have already attached the ArrayAdapter to the AdapterView. Method 2 does not, requiring you to call notifyDataSetChanged() on the ArrayAdapter.
Typically, you populate the ArrayList before creating the ArrayAdapter, then use Method 1 to add new entries dynamically later on (e.g., based on user data entry).
This is what I do, especially for my Search Results page - where it grows as the user scroll down (list changes).
I'd keep a local ArrayList of Strings that is global to the class,
Initialize the adapter (also locally and globally),
Have a designated method alter the ArrayList of strings,
Then call the adapter.notifyDataSetChanged();
This will not only update your list and also update the adapter to work in sync.
Hope this helps,
best,
-serkan

Categories

Resources