2

我已经扩展了基本适配器——我遇到了一些奇怪的问题……当我滚动这个列表视图时——我的最后一项被第一项替换,下一次当我滚动时——另一个项目和所有……有时它是正确的。 .为什么它发生在这段代码中?

@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;
    }
4

2 回答 2

2

问题是 listadapter 有时会重用这些行。如果要解决此问题,请删除以下内容:

if (convertView == null) {   <----remove
   //code
}                            <----remove
else{                        <----remove
   ...                       <----remove
}                            <----remove
于 2012-05-21T11:35:19.727 回答
0

您的getView方法看起来有点可疑,请尝试如下更改:

@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;
}
于 2012-05-21T11:29:54.550 回答