views:

2269

answers:

1

I am so confused when my list view using a BaseAdapter goes off the screen, each row no longer maintains a consecutive position. I don't know how else to explain it other than this.

If my BA/LV shows 4 items on the screen, and I add a view that displays a TextView of each row, it shows 0,1,2,3 for the row numbers (which is correct). But as soon as I scroll the list down to the bottom 4 items (items 5-8) it then shows those as 4,5,0,1?? Why?? I am so confused :/ I have tried doing all kinds of things (even some unconventional things, but this is the last thing I have that is not working).

** EDIT ** I did discover that if I change rv = (RowView) convertView; to

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);

that it works, but then it is not re-using the code. So I guess I am on the right track. I did try some convenience methods, but that did not help me too much because I needed to set those values before the Constructor fired off. Do I need to create a new method and fire that at the end? Such as 'addRow' method? This also causes it to scroll VERY Slow.

          @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);

        }
A: 

When using the convertView, you are given a view which has been previously used but which is not displayed anymore. You should take care of resetting ALL its attributes with the value of the current item. Otherwise, all the values you have set in the RowView constructor will be kept with the value you gave to this view the first time you created it.

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.

Kevin Gaudin