[android-developers] ListView items appear 2 or 3 times

2011-02-06 Thread Dirk Vranckaert
Hi all,

I'm having an issue with the ListView in Android.

So I have an activity extending the ListActivity, in the activity I
have an innerclass for my adapater.
Now both on my device and the emulator some entries appear 2 or 3
times in the list (not always the same items) although the size of my
ListLabel is always the same!

On the emulator I noticed that it's mostly related to the scrolling in
the list... If I scroll down some entries are duplicate, if I scroll
up and down again the entries have changed all over the list...

Is there anything in my code that you guys can see that is wrong?

Here's my code:


this.labels = labelService.findAll();
Collections.sort(this.labels, new LabelByNameComparator());
Log.d(LOG_TAG, labels.size() +  labels loaded!);
ManageLabelsListAdapter adapter = new
ManageLabelsListAdapter(labels);
adapter.notifyDataSetChanged();
setListAdapter(adapter);

/**
 * The list adapater private inner-class used to display the
manage labels list.
 */
private class ManageLabelsListAdapter extends ArrayAdapterLabel
{
private final String LOG_TAG =
ManageLabelsListAdapter.class.getSimpleName();
/**
 * {@inheritDoc}
 */
public ManageLabelsListAdapter(ListLabel labels) {
super(ManageLabelsActivity.this,
R.layout.list_item_labels, labels);
Log.d(LOG_TAG, Creating the manage labels list
adapater);
}

@Override
public View getView(int position, View convertView, ViewGroup
parent) {
Log.d(LOG_TAG, Getting view...);
View row = convertView;
ManageLabelsListWrapper wrapper;

if(row == null) {
Log.d(LOG_TAG, Row needs to be created!);
final Label label = labels.get(position);
Log.d(LOG_TAG, Label at list position  + position +
 retrieved from DB list:  + label);

LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_item_labels,
parent, false);
Log.d(LOG_TAG, Label row inflated into layout!);
wrapper = new ManageLabelsListWrapper(row);
Log.d(LOG_TAG, Row wrapped!);

TextView labelName = wrapper.getLabelname_listitem();
Log.d(LOG_TAG, About to update the name of the label
in the view for TextView  + labelName +  with the value:  +
label.getName());
labelName.setText(label.getName());

ImageView deleteButton = wrapper.getBtn_delete();
deleteButton.setOnClickListener(new
View.OnClickListener() {
public void onClick(View view) {
deleteLabel(label, true);
}
});
}

return row;
}
}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] ListView items appear 2 or 3 times

2011-02-06 Thread Mark Murphy
You are not handling row recycling properly. You have the if
(row==null) case, but you do nothing in the other case. You need to
bind your row widgets with the proper data in either case -- the only
difference between the two is that if (row==null), you have to create
a row, either directly via inflation or via chaining to the
superclass.

On Sun, Feb 6, 2011 at 11:39 AM, Dirk Vranckaert
dirkvrancka...@gmail.com wrote:
 Hi all,

 I'm having an issue with the ListView in Android.

 So I have an activity extending the ListActivity, in the activity I
 have an innerclass for my adapater.
 Now both on my device and the emulator some entries appear 2 or 3
 times in the list (not always the same items) although the size of my
 ListLabel is always the same!

 On the emulator I noticed that it's mostly related to the scrolling in
 the list... If I scroll down some entries are duplicate, if I scroll
 up and down again the entries have changed all over the list...

 Is there anything in my code that you guys can see that is wrong?

 Here's my code:

        
        this.labels = labelService.findAll();
        Collections.sort(this.labels, new LabelByNameComparator());
        Log.d(LOG_TAG, labels.size() +  labels loaded!);
        ManageLabelsListAdapter adapter = new
 ManageLabelsListAdapter(labels);
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);

    /**
     * The list adapater private inner-class used to display the
 manage labels list.
     */
    private class ManageLabelsListAdapter extends ArrayAdapterLabel
 {
        private final String LOG_TAG =
 ManageLabelsListAdapter.class.getSimpleName();
        /**
         * {@inheritDoc}
         */
        public ManageLabelsListAdapter(ListLabel labels) {
            super(ManageLabelsActivity.this,
 R.layout.list_item_labels, labels);
            Log.d(LOG_TAG, Creating the manage labels list
 adapater);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup
 parent) {
            Log.d(LOG_TAG, Getting view...);
            View row = convertView;
            ManageLabelsListWrapper wrapper;

            if(row == null) {
                Log.d(LOG_TAG, Row needs to be created!);
                final Label label = labels.get(position);
                Log.d(LOG_TAG, Label at list position  + position +
  retrieved from DB list:  + label);

                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_item_labels,
 parent, false);
                Log.d(LOG_TAG, Label row inflated into layout!);
                wrapper = new ManageLabelsListWrapper(row);
                Log.d(LOG_TAG, Row wrapped!);

                TextView labelName = wrapper.getLabelname_listitem();
                Log.d(LOG_TAG, About to update the name of the label
 in the view for TextView  + labelName +  with the value:  +
 label.getName());
                labelName.setText(label.getName());

                ImageView deleteButton = wrapper.getBtn_delete();
                deleteButton.setOnClickListener(new
 View.OnClickListener() {
                    public void onClick(View view) {
                        deleteLabel(label, true);
                    }
                });
            }

            return row;
        }
    }

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Warescription: Three Android Books, Plus Updates, One Low Price!

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] ListView items appear 2 or 3 times

2011-02-06 Thread Kostya Vasilyev

Dirk,

You have a bug in your getView() method.

When a list item layout gets recycled (convertView != null), your code 
correctly avoids re-inflating a new layout. However, since the item 
layout is being recycled, it's also necessary to update its views with 
values for the current item.


Pseudo-code:

getView() {

if (convertView == null) {
inflate a view
}

/* Always execute below code, for both new and recycled item layouts */

YourDataItem item = get data item specified by position

TextView view1.setText(item.name);
TextView view2.setText(item.address);
// etc
}

-- Kostya

06.02.2011 19:39, Dirk Vranckaert пишет:

Hi all,

I'm having an issue with the ListView in Android.

So I have an activity extending the ListActivity, in the activity I
have an innerclass for my adapater.
Now both on my device and the emulator some entries appear 2 or 3
times in the list (not always the same items) although the size of my
ListLabel  is always the same!

On the emulator I noticed that it's mostly related to the scrolling in
the list... If I scroll down some entries are duplicate, if I scroll
up and down again the entries have changed all over the list...

Is there anything in my code that you guys can see that is wrong?

Here's my code:

 
 this.labels = labelService.findAll();
 Collections.sort(this.labels, new LabelByNameComparator());
 Log.d(LOG_TAG, labels.size() +  labels loaded!);
 ManageLabelsListAdapter adapter = new
ManageLabelsListAdapter(labels);
 adapter.notifyDataSetChanged();
 setListAdapter(adapter);

 /**
  * The list adapater private inner-class used to display the
manage labels list.
  */
 private class ManageLabelsListAdapter extends ArrayAdapterLabel
{
 private final String LOG_TAG =
ManageLabelsListAdapter.class.getSimpleName();
 /**
  * {@inheritDoc}
  */
 public ManageLabelsListAdapter(ListLabel  labels) {
 super(ManageLabelsActivity.this,
R.layout.list_item_labels, labels);
 Log.d(LOG_TAG, Creating the manage labels list
adapater);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup
parent) {
 Log.d(LOG_TAG, Getting view...);
 View row = convertView;
 ManageLabelsListWrapper wrapper;

 if(row == null) {
 Log.d(LOG_TAG, Row needs to be created!);
 final Label label = labels.get(position);
 Log.d(LOG_TAG, Label at list position  + position +
 retrieved from DB list:  + label);

 LayoutInflater inflater = getLayoutInflater();
 row = inflater.inflate(R.layout.list_item_labels,
parent, false);
 Log.d(LOG_TAG, Label row inflated into layout!);
 wrapper = new ManageLabelsListWrapper(row);
 Log.d(LOG_TAG, Row wrapped!);

 TextView labelName = wrapper.getLabelname_listitem();
 Log.d(LOG_TAG, About to update the name of the label
in the view for TextView  + labelName +  with the value:  +
label.getName());
 labelName.setText(label.getName());

 ImageView deleteButton = wrapper.getBtn_delete();
 deleteButton.setOnClickListener(new
View.OnClickListener() {
 public void onClick(View view) {
 deleteLabel(label, true);
 }
 });
 }

 return row;
 }
 }




--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en