Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load the Listview "smoothly" in android

I load data from Cursor to listview, but my Listview not really display "smooth". The data change when I drag up and down on the scollbar in my ListView. And some items look like duplicate display in my list. I hava a "complex ListView" (two textview, one imageview) So I used newView(), bindView() to display data. Can someone help me?

like image 901
Dennie Avatar asked Aug 24 '09 04:08

Dennie


People also ask

How do I make ListView scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .

What is the use of ListView in android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


1 Answers

I will describe you how to get such issue that you have. Possibly this will help you.

So, in list adapter you have such code:

public View getView(int position, View contentView, ViewGroup arg2)
    {
        ViewHolder holder;

        if (contentView == null) {
            holder = new ViewHolder();
            contentView = inflater.inflate(R.layout.my_magic_list,null);
            holder.label = (TextView) contentView.findViewById(R.id.label);
            contentView.setTag(holder);
        } else {
            holder = (ViewHolder) contentView.getTag();
        }

        holder.label.setText(getLabel());

        return contentView;
    }

As you can see, we set list item value only after we have retrieved holder.

But if you move code into above if statement:

holder.label.setText(getLabel());

so it will look after like below:

if (contentView == null) {
   holder = new ViewHolder();
   contentView = inflater.inflate(R.layout.my_magic_list,null);
   holder.label = (TextView) contentView.findViewById(R.id.label);
   holder.label.setText(getLabel());
   contentView.setTag(holder);
}

you will have your current application behavior with list item duplication.

Possibly it will help.

like image 115
Lyubomyr Dutko Avatar answered Oct 02 '22 09:10

Lyubomyr Dutko