Why did the ListView repeated every 6th item?

Solution 1:

@Override
public View getView(int pos, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = mLayoutInflater.inflate(zeilen_Layout, null);

        //Assignment code
    }
    return convertView;
}

The reason this is happening is because of how you've defined this method (which is getView()). It only modifies the convertView object if it is null, but after the first page of items is filled Android will start recycling the View objects. This means that i.e. the seventh one that gets passed in is not null - it is the object that scrolled off the top of the screen previously (the first item).

The conditional causes you just return what was passed in to the method so it appears to repeat the same 6 items over and over.

Solution 2:

Modify you getView of list adapter as follow:

ViewHolder viewHolder = new ViewHolder();

@Override
public View getView(int pos, View convertView, ViewGroup parent) {


    if(convertView == null){
        convertView = mLayoutInflater.inflate(zeilen_Layout, null);

        viewHolder.txtTestText = (TextView) findViewById(R.id.txt_test_item);


        // Main code goes here

        convertView.setTag(viewHolder); // this function remember the the view you have inflated

    }else{
        viewHolder = (ViewHolder) convertView.getTag(); // return last set view of ith item
    }


    // set data to view here

    viewHolder.txtTestText.setText(listItem.get(postion).getSelectedItem());

    return convertView;

}




private class ViewHolder {
    TextView txtTestText;
}