This is my source code. getView shown wrong position. I already searched solution for that on google but no success. Please help me.
public View getView(final int position, View convertView,
ViewGroup viewGroup) {
final Phonebook entry = listPhonebook.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.phone_row, null);
convertView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View mycurrentListItemView,
MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
Log.i("List item clicked position : ", "" + position);
Log.i("Name::Mail", "" + entry.getName() + " :: "
+ entry.getMail());
mycurrentListItemView.setBackgroundColor(Color
.parseColor("#38ACEC"));
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
mycurrentListItemView.setBackgroundColor(Color
.parseColor("#FFFFFF"));
}
return false;
}
});
ListView is recycling the views with the convertView, so it only sets up the onTouchListener the first time the views are created (because of
if (convertView == null).If you want this solution to work, you have to move the
convertView.setOnTouchListener()call outside of theif (convertView == null)condition.This way it will be initialized every time a view is displayed on the screen with your proper Phonebook entry.