4

当我使用 a 的列表视图BaseAdapter离开屏幕时,每一行不再保持连续的位置。除了这个,我不知道还能怎么解释。

如果我的 BA/LV 在屏幕上显示 4 个项目,并且我添加了一个显示每一行的 TextView 的视图,它会显示 0、1、2、3 的行号(这是正确的)。但是,一旦我将列表向下滚动到底部的 4 项(第 5-8 项),它就会显示为 4,5,0,1?? 为什么?

编辑: 我确实发现如果我改变

rv = (RowView) convertView;

rv =  new RowView(mContext,(cursor.getString(2)),
                        (cursor.getString(5)),
                        cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME)),
                        cursor.getString(cursor.getColumnIndex(DBHelper.KEY_CITY)),position);

它可以工作,但是它没有重新使用代码。所以,我想我是在正确的轨道上。我确实尝试了一些方便的方法,但这对我没有太大帮助,因为我需要在构造函数触发之前设置这些值。我是否需要创建一个新方法并在最后触发它?比如addRow方法?这也导致它滚动非常慢。

          @Override
               public void onCreate(Bundle bundle)
               {
                 super.onCreate(bundle);
                 //setContentView(R.layout.findlist);
                 //getListView().setEmptyView(findViewById(R.id.empty));
                 mDbHelper = new DBHelper(this);
                 mDbHelper.open();
                 cursor = mDbHelper.fetchAllLocations();
                 startManagingCursor(cursor);
                 mAdapter = new myListAdapter(this);
                 setListAdapter(mAdapter);
        
               }
    public class myListAdapter extends BaseAdapter {
            public String testing;
            public myListAdapter(Context c) {
                mContext = c;
                // TODO Auto-generated constructor stub
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return cursor.getCount();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                
                 cursor.moveToPosition(position);
                 RowView rv;
    
    
                 if (convertView == null) {
                        rv = new RowView(mContext,(cursor.getString(2)),
                                (cursor.getString(5)),
                                cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME)),
                                cursor.getString(cursor.getColumnIndex(DBHelper.KEY_CITY)),position);
                    } else {
                        rv = (RowView) convertView;
                        try {
              // I KNOW THIS SECTION IS NOT RIGHT, BUT I HAVE BEEN MESSING IN HERE          
                        rv.setAddress(cursor.getString(2));
                        rv.setCity(cursor.getString(5));
                        rv.setFocusable(true);
                        rv.setClickable(true); }
                        catch (Exception e) {
                            rv = (RowView) convertView;
                            rv.setAddress(cursor.getString(2));
                            rv.setCity(cursor.getString(5));
                            rv.setFocusable(true);
                            rv.setClickable(true); 
                            Toast mToast;
                            mToast = Toast.makeText(FindList.this, "Error :" + e.toString() ,
                                    Toast.LENGTH_LONG);
                            mToast.show();
                        }
                    }
                    
                 return rv;
            }
            
            public void addItems() {
    
                 //String[] from = new String[] { DBHelper.KEY_BUSINESSNAME, DBHelper.KEY_ADDRESS, DBHelper.KEY_CITY, DBHelper.KEY_GPSLONG, DBHelper.KEY_GPSLAT,  DBHelper.KEY_IMAGEFILENAME  + ""};
                 // create array of values of widgits
                 //to = new int[] { R.id.businessname, R.id.address, R.id.city, R.id.gpslong, R.id.gpslat, R.id.preview};
                 // Now create an array adapter and set it to display using our row from notes_row.xml
                 
                 
            }
            
            
            
        }


private class RowView extends LinearLayout {
        private TextView mAddress;
        private TextView mCity;
        public ImageView mArrow;
        public ImageView mPicture;
        public String mPathName;
        public String mDateTime;
        public RowView(Context context, String title, String words, String pathName, String city, int position) {
            super(context);

            this.setOrientation(HORIZONTAL);
            this.setVerticalGravity(16); //CENTER_VERTICAL 

            // Here we build the child views in code. They could also have
            // been specified in an XML file.
           
            //DISPLAY DELETE BUTTON
            Button mButton = new Button(context);
            mButton.setFocusable(false);
            mButton.setId(position);
            mButton.setBackgroundResource(R.drawable.delete3);
            addView(mButton, new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            
            TextView mTitle;
            mTitle = new TextView(context);
            mTitle.setText(Integer.toString(position));
            addView(mTitle, new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            this.setOrientation(HORIZONTAL);      
            
            try {
             Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME))),100, 100, true);
                mPicture = new ImageView(context);
                mPicture.setImageBitmap(bm);    
           } catch (Exception e) { 
                 mPicture = new ImageView(context);
                 mPicture.setImageResource(R.drawable.noimage);
           }     
            
            addView(mPicture, new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            
            mArrow = new ImageView(context);
            mArrow.setBackgroundResource(R.drawable.arrowleft3);
            addView(mArrow, new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          
            currentPosition = position;
            Button button = (Button)findViewById(position);
            button.setOnClickListener(mCorkyListener);
            
        }
4

1 回答 1

2

使用 convertView 时,您会得到一个以前使用过但不再显示的视图。您应该注意使用当前项目的值重置其所有属性。否则,您在 RowView 构造函数中设置的所有值都将与您第一次创建此视图时提供的值保持一致。

To better understand ListViews, you have to understand that it only uses a limited set of RowViews, just the number that is enough to fill the display and a few more that will be filled with your data just before having to display them.

于 2010-09-06T23:55:39.597 回答