Android :: ListView Large Number Of Items - Best Practices
Oct 20, 2010
I am working with a ListView, custom adapter and a large number of items. I read in a book for Android that is was more efficient to use what it called the holder pattern. That is to create a wrapper class for each view in the list view that cached the objects in the view so as to avoid calls to findViewById because those are supposed to be expensive. My question is what is better? To have 50,000 objects GC'd every time the user scrolls or to make the 4 or five calls to findViewById per view? Below is my implementation of what the book suggested.
@Override public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView; if (view == null) {
final LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.survey_item, null);
view.setTag(new SurveyItemWrapper(view));
} bindView((SurveyItemWrapper) view.getTag(), position);
return view; } private void bindView(SurveyItemWrapper surveyItemWrapper, int position)
{ final SurveyedItem surveyedItem = surveyedItems.get(position);
surveyItemWrapper.getDescription().setText(surveyedItem.getItemName());
surveyItemWrapper.getCube().setText(String.format("%9.2f", surveyedItem.getCube()));
surveyItemWrapper.getShipping().setText(String.format("%d", surveyedItem.getShipping()));
surveyItemWrapper.getNotShipping().setText(String.format("%d", surveyedItem.getNotShipping()));
} private class SurveyItemWrapper { private TextView description;
private TextView cube; private TextView shipping;
private TextView notShipping; private View view;
public SurveyItemWrapper(View view) { this.view = view;
} public TextView getDescription() {
if (description == null) { description = (TextView) view.findViewById(R.id.SurveyItemDescription); } return description;
} public TextView getCube() { if (cube == null) {
cube = (TextView) view.findViewById(R.id.SurveyItemCube);
} return cube; } public TextView getShipping() {
if (shipping == null) { shipping = (TextView) view.findViewById(R.id.SurveyItemShipping);
} return shipping;} public TextView getNotShipping() {
if (notShipping == null) { notShipping = (TextView) view.findViewById(R.id.SurveyItemNotShipping); } return notShipping; } }
View 2 Replies
Jun 27, 2010
I have a ListView with a scrolling issue. The Array contain a large number of items. When I try to scroll down, the listview displays the first items again. For example, if the initial view shows 12 items and I scroll down to view the next three, instead of showing 4 through 15, if shows 4 through 12 followed by 1, 2, and three. Debugging the Adapter reports that getView return 4 through 15 just as I would expect.
View 3 Replies
View Related
Mar 4, 2010
How can i set the number of items to show in a list without to scroll?
Example: I have a list with 10 items.
I want that only 3 items appears and that I have to scroll to see the rest of the items.
View 1 Replies
View Related
Aug 16, 2010
I have a ListView backed by a custom adapter based on a CursorAdapter.The cursor I give it is based on a list of Notes in the database (each note is a DB row).Everything works just fine until I delete a note from the database. I'm not sure how to properly update the cursor/adapter/listview to display the new data.I've read conflicting posts on using adapter.notifyDataSetChanged() vs cursor.requery() to refresh the UI, but neither of them seems to work in this case.The only way I've been able to refresh it is by creating a new cursor from a new query and calling adapter.changeCursor(newCursor).Could someone demonstrate the proper way to update the backing data and then the UI (with psuedocode if possible).
View 2 Replies
View Related
Oct 9, 2010
I have a multiselection listview in my android app. What I need to know is how many items on that list I have selectet.
View 1 Replies
View Related
Aug 26, 2010
I have an activity with multiple list views that are continuously receiving new values form a socket thread, another thread parses the data and updates the array adapters, then the ui thread calls notifyDataSetChanged() to cause the list to refresh.
My issue is that im refreshing all the list a couple of time a second, this causes the UI to be very laggy when some animations need to happen.
I was wondering what the best way is to update multiple lists with multiple value changes every second?
View 3 Replies
View Related
Sep 30, 2009
There seems to be a limit to the number of SMS that can be sent programmatically. It seems to be hovering around 70 to 100 messages. After a batch of messages, when I try to resend a batch I get a warning message "A large number of SMS are being sent. Press OK to continue or Cancel to stop sending" This requires user intervention and impedes the program from sending SMS. Is there a way around this error message? Maybe a way to turn off the warnings?
View 3 Replies
View Related
Sep 11, 2010
I have some 34 checkboxes on one Activity which i have to mark and unmark. and I want to save the status of the each checkbox.
I want this persistently. So should i have a sqlite table or preferences ?
If sqlite table is recommended, then what should be the ideal table structure design?
View 1 Replies
View Related
Aug 11, 2010
I have no idea, but my app is stalling sometimes completely (not responding to touching the screen) I thought I was following all the rules for threading, but my app is apparently slowing down the Sprint EVO! I have never used an app as slow as my own. I don't get an ANR dialog, though I think I probably should get getting one. I have no idea how to make this application run like every other app I've ever used -- Not freezing. Is it not a good idea to have one HttpClient and keep a static reference to it for all Activities that want to use it? I used to create a new one each time, and thought that changing it to one sped it up. None of my AsyncTasks ever end. If I go to Activity A -> B -> C -> D the first time it will be smooth. I can press back but when I get back to A, its completely frozen, not responding to anything. That when I took that screenshot. I really have no clue what I am doing wrong. Should I manually kill my AsyncTasks on Activity.onPause()?
View 1 Replies
View Related
May 28, 2010
I'm just getting my feet wet with Android and have built a UI that contains a TabHost with three tabs. Each tab is powered by its own Activity. The first Tab contains a listview with a prepopulated set of rows and is built from a custom ArrayAdapter.
The problem I'm running into is that none of the ListView rows are tappable. In other words, when I tap on them there is no orange selection. If I use the scroll ball on my Nexus One it will select, but any touch gestures don't seem to be responding. All the UI is being handled using XML files with a main.xml housing the TabHost -> LinearLayout -> TabWidget/FrameLayout and a nearby_activity.xml file containing my ListView UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"............
View 1 Replies
View Related
Aug 27, 2009
I cannot get the data from SqLite using order by because the data is encrypted, so the sort wouldn't work there. Is it possible to sort the items in the ListView?
View 5 Replies
View Related
May 15, 2009
I've noticed after I've switched to 1.5 that if ListView item is not enabled the divider(s) for this item are not rendering. Is this intentional?
View 7 Replies
View Related
Jun 19, 2010
I want to achieve this kinda of row look in my app. I already have a ImageView/TextView layout, but I want that 'positive' little image in the right always being displayed at the exact same place!
View 1 Replies
View Related
Aug 16, 2010
When this displays the first two rows of the listview appear under the tabs, the Mercury, and Venus planets. How do I get the tabs to occupy the top and the listview rows to start after the tabs?
tablistmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
<TextView
android:id="@+id/textview4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lstMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</TabHost>
tablistmenu.java: public class tablistmenu extends TabActivity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tablistmenu);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Contacts", getResources().getDrawable(R.drawable.contact_32)).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Credit Cards", getResources().getDrawable(R.drawable.credit_card_32)).setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Notes/Misc", getResources().getDrawable(R.drawable.notes_32)).setContent(R.id.textview3));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Websites", getResources().getDrawable(R.drawable.globe_32)).setContent(R.id.textview4));
mTabHost.setCurrentTab(0);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.lstMain);
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list. listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Set the ArrayAdapter as the ListView's adapter. mainListView.setAdapter( listAdapter );
// End
} } }
View 3 Replies
View Related
Aug 30, 2010
How to add items to a ListView in Android during runtime?
View 1 Replies
View Related
Jul 20, 2010
I have a listview that's using a custom adapter. I want to dynamically show/remove items from the listview. I've tried everything inside the getView() method in my view adapter. I've tried doing setVisiblity(View.GONE) on the view I'm returning. And it infact doesn't draw the view, but it allocates space for the view and it's just a blank black space.
Is this even possible to set items in listview invisible?
View 2 Replies
View Related
Nov 8, 2010
I seem to be having a problem with Android ListView. I'm trying to delete the selected item from ListView. The delete method is working and will allow me to delete as many items from my list as needed, but whenever I scroll to the bottom of the list (after atleast 1 item has been deleted) I get an Error that closes the Application.
Here is my code:
CODE:.............................
View 2 Replies
View Related
Nov 12, 2009
I want to change the max number of items in the OptionsMenu (3 visible options and More option available). Is that possible?
View 2 Replies
View Related
Apr 4, 2012
I have some class LoginActivity.java. In the onCreate method I retrieve ListView:
ListView list = (ListView)findViewById(R.id.snListView); then:
list.setAdapter(adapter);
In addition there are login.xml layout - there are:
<ListView
android:id="@+id/snListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
and sn_row.xml:
[Code]...
At the moment I have cell's height 100dp, I need that cell's height will depend on device, I mean I have here 5 cells showing at login screen in ListView and I need that these 5 rows will fit the ListView (cell's height = ListView's height/5).
View 1 Replies
View Related
Sep 23, 2010
Im using a custom listView with a Title and a Subtitle where you can read a brief explanation of the item.
For each item on the list, im displaying an alertDialog to select an option (different for each case). When the option is selected, i want to change the Subtitle for the option selected by the user.
This is what i tried:
CODE:.....................
For the first item on the list it works fine, when i select an option, the subtitle get replaced by that option, but when i make a selection in the alertDialogs of the other 2 items, the option selected replaces the subtitle of the first item!
View 1 Replies
View Related
May 31, 2009
After the listview is already initialized, is it possible to add items to it during runtime?
View 3 Replies
View Related
Apr 1, 2010
Can somebody please give me an example code of removing all ListView items and replacing with new items. I tried replacing the adapter items.Still no results.
My code is:
at first I am calling
populateList(){
results -populated arraylist with strings
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, results);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
View 6 Replies
View Related
Feb 3, 2009
I want to set the onclicklistner on the each item to be displayed in ListVIew. The code I have written is:
package munish.android;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class List extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an existing ListAdapter that will map an array
// of strings to TextViews
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
getListView().setTextFilterEnabled(true);
} private String[] mStrings = {
"ImageView", "Grid View", "List View", "Map View", "Image Switcher", "Queso Jalapeno", "Queso Majorero", };
View 4 Replies
View Related
May 26, 2009
I'm trying to make a list that contains elements not necessarily occupying the entire width of the screen, but even if using android:layout_width="wrap_content" on my elements, they always behave as if their width value were set to "fill_parent" instead. If I set a fixed value for the width, e.g. "100px", that value is indeed used, but I just can't get wrap_content to work.
Example simple list item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#707"> <TextView android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentRight="true"
android:background="#770" android:text="Text1" /> <TextView android:id="@+id/text2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_below="@id/ text1"
android:background="#077" android:text="Text2" /> </RelativeLayout>
What am I missing here? How do I accomplish a list item with wrap_content behaviour, i.e., where the width of each RelativeLayout is adapted to the length of the strings in the contained text views?
View 3 Replies
View Related
Dec 11, 2009
My app starts with a blank listview with a Menu option to Add contacts to the ListView. Now, I want that my ListView gets saved every time I quit my App so that the next time I start my App, I see all the contacts from last time. I understand that Android lets you store and retrieve data. Just don't know how to move forward from here.
View 2 Replies
View Related
May 6, 2010
Given a listview that shows checkboxes next to a list of people, I want to be able to get the names (in Strings for example) of the people who are checked. I have set my listview mode to allow for multiple checks.
getCheckedItemPositions()
Will get the positions but I can't figure out how to iterate over the listview to get the names. Also Eclipse tells me that getCheckItemIds is not a valid method for ListView.
View 3 Replies
View Related
Jan 28, 2010
I have to drag and drop items of ListView, which typically have an image and text. How can I implement this.
View 3 Replies
View Related
Jun 24, 2010
I'm building a list that is sorted alphabetically. The list should show a divider containing the Letter that the following icons belong to.
I'm using a Cursoradapter that contains the already sorted result from the database. I plan on adding the divider to the list item and set it to visible if the shown letter changes. How do I know that I have a new Letter? Is there a better way then going forward or backward in the cursor and check if the item I show is the end or beginning of a new group?
View 1 Replies
View Related
Nov 3, 2010
I've a ListView that can have 0 custom items inside (like "My Downloads").Is there anyway to show a default text "No download yet" ?
View 2 Replies
View Related
Dec 16, 2009
The way I want it is, per row there will be a text and is followed by a right arrow button, so that the user can click on that button to edit the content of that row. If the user is using the keypad, by using the left and right buttons on the keypad, he should be able to select the textview or the Button in that particular row. I have seen this in a couple of apps. The way I see that working on those apps appear as if they are maintaining two lists in parallel.
The reason i felt it this way is, if I am on row 1 of the list and if I press the right key on the phone keypad, then the right arrow button will get highlighted and now if I use the down key or if I keep pressing the down key, then the focus will scroll through that arrow button only as if that is an another parallel list constructed. I don't know if that is a good way to do it. I tried many for getting this to work, but couldn't succeed.
View 4 Replies
View Related