How to clear the ExpandableListView? - android

I am using expandable list view as a tree view structure and it needs to be updated continuously but as I can not clear the current expandable list view, it shows the previous data along with new data. Like if I minimize and again open app there is duplicate data in list view.

yourExpandableListView.setAdapter((BaseExpandableListAdapter)null);

ExpandableListAdapter adapter = new ExpandableListAdapter(); //or what ever adapter you use/created listView.setAdapter(adapter)
Please try this

Related

SwipeMenuListView new Row added is showing First time data

I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .

Add footer to an Expandable ListView

I have an expandable listView which already has some data(from database). When user opens my application, a "loading" header is displayed during which new data is pulled from the internet.
Now when the new data is received , I store it in a database and update my cursor. New data is displayed above old data.
I also have a footer which asks whether i have to load more data from the internet. Now , the footer is displayed below the old data. I want a view in between the new data and old data, i.e. below new data , so that when clicks on that view , new data is pulled from the internet , which is pushed below new data but above old data. (You can imagine twitter timeline example for understanding my problem.)
How do i implement this ?
I don't know if it will work exactly as you expect, but try MergeAdapter (link below) with added View. You will have one ListView and two same adapters - one for the old and for the new data. Between will be some View - in your case loading Button.
MergeAdapter adapter = new MergeAdapter();;
adapter.addAdapter(oldAdapter);
adapter.addView(loadingLayout);
adapter.addAdapter(newAdapter);
MergeAdapter enables to show (something like) two ListViews in single activity with single ScrollBar. But there is/was some problem with removing added Views. Check it out if this option is enabled.
Link to MergeAdapter: https://github.com/commonsguy/cwac-merge
Maybe this will be in some case helpful: http://pivotallabs.com/users/joe/blog/articles/1759-android-tidbits-6-22-2011-hiding-header-views

Setting tags to each item in a ListView in Android?

I have a ListView where I want each item to have an ID number attached to it (not the same as the position number). I was hoping this could be done by setting a tag to each View item in the ListView using setTag() when these Views are being created.
Right now I'm creating the ListView like this:
final ListView listview = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
listview.setAdapter(adapter);
The names variable in the ArrayAdapter parameters above is an ArrayList, and each string value in this list also has a unique ID that I want to link to this string somehow.
Is there any way I can get access to and modify each of the Views with a tag? One idea was to create my own extended class of ArrayAdapter and override the getView() method, but I don't really understand how it works and how I would go about doing this.
Or is there a better way to link IDs with each string like this than adding tags like I'm trying to do?
Create a ViewBinder and set the tags as the ListView is being populated with whatever you need. You can check all properties of the view to determine what tag goes where, so this should be what you're looking for.
myAdapter.setViewBinder(new MyViewBinder());
public class MyViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Object data, String text){
//Since it iterates through all the views of the item, change accordingly
if(view instanceof TextView){
((TextView)view).setTag("whatever you want");
}
}
}
I just used this exact same answer on another question (albeit slightly different) yesterday.
about getView , it works by using a method of recycling views. i will try to explain it in a simple way.
suppose you have tons of items that can be viewed . you don't want to really create tons of views too , since that would take a lot of memory . google thought of it and provide you the means to update only the views that need to be shown at any specific time.
so , if there is an empty space on the listview , it will be filled with a new view . if the user scrolls , the view that becomes hidden is recycled and given back to you on the getView , to be updated with the data of the one that is shown instead .
for example , if you scroll down , the upper view becomes hidden for the end user , but in fact it becomes the exact same view that is on the bottom .
in order to understand how to make the listview have the best performance and see in practice how and why it works as i've talked about , watch this video:
http://www.youtube.com/watch?v=wDBM6wVEO70
as for tags , i think you want to do something else , since the data itself (usually some sort of collection, like an arrayList) already knows where to update , because you get the position via the getView . if you want a specific view to update , you might be able to do so by using a hashmap that keeps upadting , which its key is the position in the collection , and the value is the associated view . on each time you go to getView , you need to remove the entry that belong to the view (if exists) and assign the new position with the view that you got/created .
Thanks for the answers. thisMayhem's answer would probably have been easier in the end, but on my quest to learn more I ended up making my own adapter according to this tutorial. I pass down the names and the IDs into the adapter and set the names as the text of the TextViews and the IDs as the tags.
I would rather go with the solution discussed in this thread. It is always the easiest to have all related data in same place and in this case you just create a class to hold all the information you will need for every item.

ListView refresh taking it to the top of the list

Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.
Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.
Current code: This is called every time I fetch a new data.
mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);
Instead of your code please try to put every time you fetch data this one:
adapter.notifyDataSetChanged();
Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.
Hope this helps!
As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this
mListAdapter.notifyDataSetChanged();
You can use
mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);
This way the listview will not refresh to the top.
Or you can also use
adapter.notifydatasetchanged
by updating the binded list.
This Worked For Me:
adapter.setData(list);
adapter.notifyDataSetChanged();

Android: Possible solution for updating the custom list view at random times

I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn't work, similarly I even tried having a timer to update my list after sometime, that didn't work either. Please let me know of possible solution.
Update:
This is how I am making my custom list view
li= (ListView)findViewById(R.id.id_lv_row);
ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
registerForContextMenu(li);
li.setDivider(divcolor);
li.setDividerHeight(2);
li.setAdapter(new FriendsPositionAdapter(this));
BaseAdapter.notifyDataSetChanged() should do the trick as long as the data behind the adapter actually changed. That's all you need to do to refresh the list.
Invalidate is for repainting views only, you have to tell to the List adapter (BaseAdapter) that dataset has changed.
When the data changes, asign the new dataset to the adapter, and later call notifyDataSetChanged()...
in order to make functional notifyDataSetChanged() the adapter data must be changed. Remember that the original data that change is not reflected automatically to the adapter.
//here i retrieve the new list, named "beans"
lista = (BeanList) result.getDataObject();
Vector<Bean>beans = list.getBeanList();
((BeanListAdapter)listAdapter).syncData(beans);
((BeanListAdapter)listAdapter).notifyDataSetChanged();
//now the syncData method
public void syncData( List<PINPropiedad> newData ){
for(Object o : newData){
add(o);
}
}

Categories

Resources