I am so confused when my list using the BaseAdapter is disconnected from the screen, each line no longer supports a sequential position. I do not know how else to explain this except this.
If my BA / LV shows 4 elements on the screen, and I add a view that displays a TextView of each line, it shows 0,1,2,3 for line numbers (which is correct). But as soon as I scroll to the bottom 4 points (points 5-8), it shows them as 4,5,0,1 ?? What for?? I am so confused: / I tried to do all kinds of things (even some unconventional things, but this is the last thing I have that does not work).
** EDIT ** I found 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 does not repeat the use of code. So I think I'm on the right track. I tried some convenient methods, but that did not help me too much, because I needed to set these values before the constructor worked. Do I need to create a new method and run it at the end? For example, the addRow method? It also makes it scroll through the VERY Slow.
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
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;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
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 {
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() {
}
}
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);
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);
}