Sanjay Herle
Sanjay Herle

Reputation: 323

ListView Jumbled on scroll

i have extended Base Adapter --- i am having some strange problem... when i scroll this listview -- my last items are replaced with first item and next time when i scroll - another item and all.. sometime it is correct.. why its happening with this code ?

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // sets the view onto list view
        LinearLayout rowLayout = null;

        System.out.println("getView " + position + " " + convertView);
        rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.apps_list_row_items, parent, false);
        if (convertView == null) {
            // inflating the row

            vh.mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
            vh.mAppName = (TextView) rowLayout.findViewById(R.id.application);

            vh.mAppHint = (TextView) rowLayout.findViewById(R.id.hint);

            mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
            mDownloadButton.setFocusable(false);

        } else {
            // convertView.getTag();
            rowLayout = (LinearLayout) convertView;
        }
        // convertView.getTag();

        // On Click of the download button which triggers an async to download
        // the file.


        mIcon = mAppIconMapList.get(position);
        System.out.println("Icon " + mIcon);
        mCurrentApplication = mAvaiableApps.get(position);
        System.out.println("Current App " + mCurrentApplication);
        vh.mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
        vh.mAppName.setText(mCurrentApplication.replace(".apk", ""));
        vh.mAppHint.setText("Click here to view Description");

        return rowLayout;
    }

Upvotes: 2

Views: 392

Answers (2)

Sebastian Breit
Sebastian Breit

Reputation: 6159

The problem is that the listadapter is reusing the rows sometimes. If you want to fix this, remove the following:

if (convertView == null) {   <----remove
   //code
}                            <----remove
else{                        <----remove
   ...                       <----remove
}                            <----remove

Upvotes: 2

waqaslam
waqaslam

Reputation: 68187

Your getView method looks a little suspicious, try changing it as below:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
        // sets the view onto list view

        ViewHolder vh = null;


        System.out.println("getView " + position);

        if (convertView == null) {
            // inflating the row
            LinearLayout rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.apps_list_row_items, parent, false);

            vh.mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
            vh.mAppName = (TextView) rowLayout.findViewById(R.id.application);

            vh.mAppHint = (TextView) rowLayout.findViewById(R.id.hint);

            mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
            mDownloadButton.setFocusable(false);

            convertView.setTag(vh);

        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        // On Click of the download button which triggers an async to download
        // the file.


        mIcon = mAppIconMapList.get(position);
        System.out.println("Icon " + mIcon);
        mCurrentApplication = mAvaiableApps.get(position);
        System.out.println("Current App " + mCurrentApplication);
        vh.mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
        vh.mAppName.setText(mCurrentApplication.replace(".apk", ""));
        vh.mAppHint.setText("Click here to view Description");

        return convertView;
}

Upvotes: 0

Related Questions