I am working on an Android project. In my app,I have 3 tabs. As the Activities I use have many things in common (e.g. ListView), I use inheritance as below:
List a is generated by my SAXHandler and it creates a new list in startDocument ().
I have a testcase of which the list in A_Activity has 7 items and B_Activity has 3 items. When my app starts, I have no problem clicking on all the items shown in A_Activity. After I click B_Activity and switch back to A_Activity, I also don't have any problem clicking on the first 3 items; however, if I click on the fourth items, I get
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
If I increase the number of items on B_Activity to 4, I get the same exception with 3 replaced with 4. So, I am certain that the cause of it is that when I jump back to A_Activity from B_Activity, the list in A_Activity is still referring to B_Activity's.
Your advise will be highly appreciated. Thank you!
You can do simple thing just clear the adapter data and call notifyDataSetChanged of an adapter class each time you switch activity.
Tab is started with 0. So start with 0.
tabhost.setCurrentTab(0);
1 means->0
2 means->1
and so on
Related
I have came across these type of questions a lot, But none of the answers solved my problem.
I am developing a mobile shopping application model. I have a custom ListView with an ImageView, and EditText(quantity), 2 Buttons for increasing and decreasing the quantity, 3 TextViews one for item Name and other two for prices and I am dynamically loading a number of Buttons for NetWeights based on the requirement through Java code and I am implementing it using CustomAdapter(Class) extending BaseAdapter and getView().
Problem 1:
When I use the condition if(convertView==null), setTag(holder) and getTag(), the dynamically loaded Buttons are getting duplicated and gets doubled in number.
Problem 2:
While scrolling the custom ListView I am loosing my data on 2 of the TextViews while the other TextView(name) is fine because in that TextView I don't Change data on runtime. On the other two TextViews(prices) I change the data on runtime using ButtonClicks(increase and decrease) where at that time when I scroll the two TextViews, it looses its content. Actually I had this same problem with the EditText(displaying quantity), I solved it using the TextWatcher by getting the value and passing it in an array and again setting it. I used the same technique (i.e) the TextWatcher for the TextViews But that did'nt Help. I also tried removing the TextWatcher and setting the text with the array and adding TextWatcher. Still I am loosing my content in those TextViews
Any Suggestions??????
Problem 1:
While using ViewHolder (setTag(), getTag()) we will be reusing the views.
For example: Let's say we have 10 listview items and the screen can display 3 items at a time. Lets say we add 2 buttons to list item 1. First time when we load the listview we will see two buttons. Lets say now we scroll down and come back to the list item 1 again, as we are reusing the views, we already have 2 buttons and if we have the code to add buttons using getTag() then 2 more buttons will be added. I think this may be causing the issue of button duplication. To add little more information, list item 1, list item 4, list item 7 and list item 10 will all use same view, as our screen in this example can display just 3 items.
Problem 2:
One idea is to store all these text values in the arrayList.
For example, lets assume we have one textView inside each list item that can be incremented/ decremented. Set textWatcher on this textView and whenever text changes add it to the arrrayList using 'position' as the index. Inside getView method when you do getTag() try to set the textView using previously stored text (that was stored inside the arrayList)if exists.
is it possible to make an expandable list within an expandable list
i am making a recipe application and i've three expandable lists of the same type - vitamins, minerals and macro-nutrients created with expandablelistview with android studio
is it possible to put these three expandable lists under an expandable list called nutrition and if so could you briefly outline how please. thanks in advance
Short answer, yeah you can but you really really shouldn't.
I've tried this before and it didn't end well, I can't remember the exact reasons but conflicts arise between touch listeners and the measure passes when trying to layout the views inside the different adapters don't calculate correctly.
You can find a number of custom ExpandableLists trying to do what you want around the web but I found they also had similar issues.
What I ended up doing was building my own expandable listview. If you check the source code for Android's ExpandableListView and ExpandableListAdapter you'll find that it's basically
just a regular listview
a list of items that are visible
an Async filter for the data source
some meta data to tell if its a child or group, expanded or collapsed, etc
So if all your item are collapsed the visible list only contains the Group Items.
Expand a Group Item and the async filter gets triggered and populates the visible list with the Group Items and the Children of the expanded Group Item.
Depending on the meta data it will call either getGroupView or getChildView.
So if you want you can take a shot at writing your own.
I wrote a blog post for an ExpandableList that I wrote that does, in theory, infinite levels of expansion. NLevelExpandableListView
I am displaying data: 3 fields for each record, 3 ListViews are currently displaying the corresponding data.
I did this so that I could have columns for the data.
The problem is: When you have enough data that scrolling is neccassary, I need to sync the ListViews.
Can anyone point me in the right direction? Or is there a better solution for displaying this data in column format?
Thanks
You could try registering a scroll listener on each of the lists (http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html)
Then update the position of the other 2 lists whenever that gets called back (either http://developer.android.com/reference/android/view/View.html#scrollTo(int, int) or http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int))
I wouldn't recommend this though as it will not scroll smoothly or at the same time!
As Ken mentioned in a comment a single list with a custom view would work much better - consider a LinearLayout with a weightSum of 3 and your 3 views (or sub-layouts) all having a weight of 1, to get 3 equally spaced columns.
Hi am developing an android application and i want to implement the pagination while displaying a list view into an activity.
I had seen many example related to this but none has solved my problem. That's why i posted this question here.
for implementing listview into the application i had developed a custom class which extending extends ArrayAdapter<BoObject> and am passing a List < MyCustom Class > into this like
new MyListAdapter(CurrentScreen.this, R.layout.show, customobjects);
For iteration I need to use the customobjects list am first getting , and i don't want to querying for server each time the more button gets clicked.
Can anyone please give a solution for my issue.?
You can try the ViewPager (use it both on Gingerbread [needs to include the supportV4 package] or on ICS), this is used when you want to scroll from left to right by a gesture, even with this ViewPager you can directly snap to a page. On the other side, you can use a ViewSwitcher
too.
Both solutions lets you define a bunch of "Pages", with both View objects you can set the current page and so you can navigate forward or back to the previous page.
there is a solution :
get 100 items in each request from server , insert them to database (sqlite) and read from database every 10 item then display them in list (pagination 10) .
when there is no more item in db send last item id to server and request for next 100 items from server . save them to db and do the routine again .
hope this helped .
I have a JList that gets filled up with values with a DefaultListModel.
By entering a name and pressing on a button it gets listed in the JLlist (vertical wrap).
But the odd thing is they get placed underneath each other properly (8 items).
But starting from 8 items suddenly al the items get listed right of it again (like shown below).
The JList itself is more then big enough, so what could be causing this most odd behaviour?
All the items below get stored in a list and are being added to the default list model.
1 9
2 10
3
4
5
6
7
8
But the proper output should be everything listed underneath each other.
Read the JList API and follow the link to the Swing tutorial on "How to Use Lists". There it will explain about the "setLayoutOrientation" method which supports 3 values:
Vertical
Vertical Wrap
Horizontal Wrap
You must have set this property somewhere in your code.