Как настроить Listview с более чем 16 элементами в GetView с держателем

У меня проблема с getView при добавлении элементов в ListView с помощью BaseAdapter. Когда я сохраняю «от 0 до 15 элементов», например, «if(position == 0 ) до if(position == 15)», он последовательно отображает все элементы ListView. Когда я пытаюсь добавить еще один (16-й) элемент в ListView (например, if(position == 16)), он отображает первый элемент в ListView и не показывает 16-й элемент, который я добавил в массив.

Я использую пользовательскую строку ListView, и каждая строка имеет свою собственную функцию, которая взаимодействует с пользователями. Но я не знаю, как добавить более 16 элементов в getView, используя «позицию».

У меня есть список из 21 элемента, но я не могу реализовать более 16 элементов.

У кого-нибудь есть решение этого, чем, пожалуйста, скажите мне

Получить код

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder;
    TextView tv1;
    ImageView img;
    TextView desc;

    LayoutInflater inflater = context.getLayoutInflater();

    if(convertView == null){

     convertView = inflater.inflate(R.layout.screenlock_addapter, parent,false);
    holder = new Holder();

    if(position == 0 ){
    holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
    holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
    holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
    holder.htv.setText(Listitem[position]);
    holder.hdesc.setText(desc1[position]);
    holder.himg.setImageResource(aimg[position]);
    convertView.setTag(holder);
    }
    if(position == 1 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.htv.setText(Listitem[position]);
        convertView.setTag(holder);
        }

    if(position == 2 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
        if(position == 3 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 4 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 5 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 6 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 7 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 8 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 9 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 10 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 11 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 12 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 13 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == 14 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }

    if(position == 15 ){
            holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
            holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
            holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
            holder.htv.setText(Listitem[position]);
            holder.hdesc.setText(desc1[position]);
            holder.himg.setImageResource(aimg[position]);
            convertView.setTag(holder);
            }

       if(position == position + 1 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
       if(position == position + 2 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == position + 3 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == position + 4 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
    if(position == position + 5 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }

    }
     else {
            holder = (Holder) convertView.getTag();
        }


    return convertView;
}

person hardwork    schedule 15.05.2015    source источник
comment
поделись кодом адаптера   -  person amodkanthe    schedule 15.05.2015
comment
Я пробовал все, но отображалось только 16 элементов. Я пробовал выше позиции +1 и все, но это не сработало.   -  person hardwork    schedule 15.05.2015
comment
@Manish - любое решение для этого...   -  person hardwork    schedule 15.05.2015
comment
покажите метод getCount() пожалуйста   -  person D4rWiNS    schedule 15.05.2015
comment
Сначала вам нужно оптимизировать свой код. Используйте регистр переключения. Всегда старайтесь публиковать чистый код, чтобы люди могли его понять. Покажите место, откуда вы устанавливаете данные для адаптера.   -  person Manish    schedule 15.05.2015
comment
метод @Override public int getCount() { // TODO Auto-generated method stub return Listitem.length; }   -  person hardwork    schedule 15.05.2015


Ответы (4)


Попробуйте этот код...

public View getView(int position, View convertView, ViewGroup parent) { // TODO Автоматически сгенерированный метод-заглушка Holderholder; ТекстПросмотр ТВ1; ИзображениеПросмотр изображения; описание TextView;

LayoutInflater inflater = context.getLayoutInflater();

if(convertView == null){

 convertView = inflater.inflate(R.layout.screenlock_addapter, parent,false);
 holder = new Holder();

if(position == 1 ){
    holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
    holder.htv.setText(Listitem[position]);
    convertView.setTag(holder);
}

else{
    holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
    holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
    holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
    holder.htv.setText(Listitem[position]);
    holder.hdesc.setText(desc1[position]);
    holder.himg.setImageResource(aimg[position]);
    convertView.setTag(holder);
    }

}
 else {
        holder = (Holder) convertView.getTag();
    }


return convertView;

}

person Rohit    schedule 15.05.2015
comment
Нет, он все еще не работает, показывает только 16 элементов (от 0 до 15). на 17-м элементе отображается первый элемент.. - person hardwork; 15.05.2015
comment
первый элемент должен начинаться с позиции listview 0, но с вашим кодом его начальная позиция listviw с 1. он отображает 0 элемент в позиции 17th - person hardwork; 15.05.2015
comment
если доступно другое решение, пожалуйста, напишите. - person hardwork; 15.05.2015
comment
Используя ваш код, когда я перехожу к последней строке списка и возвращаюсь к первой ... он меняет положение строки и отображает представление в непоследовательности. - person hardwork; 15.05.2015

Вы меняете информацию только в том случае, если позиция равна 15 или меньше, если вы хотите сохранить это, вы должны реализовать это:

if(position == 16 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }
if(position == 17 ){
        holder.htv = (TextView) convertView.findViewById(R.id.scl_tv);
        holder.himg = (ImageView) convertView.findViewById(R.id.scl_img);
        holder.hdesc = (TextView) convertView.findViewById(R.id.scl_desc);
        holder.htv.setText(Listitem[position]);
        holder.hdesc.setText(desc1[position]);
        holder.himg.setImageResource(aimg[position]);
        convertView.setTag(holder);
        }

...так далее

ОПАСНОСТЬ: ЭТО ОЧЕНЬ ПЛОХАЯ ПРАКТИКА, ВЫ НЕ ДОЛЖНЫ ЭТОГО ДЕЛАТЬ! ВЫ ДОЛЖНЫ ВЫПОЛНИТЬ РАБОТУ ТОЛЬКО 1 РАЗ

person D4rWiNS    schedule 15.05.2015
comment
Нет, я не буду отображать 21 элемент в своем списке. 16 элементов уже отображаются, но я хочу, чтобы после этих 16 элементов отображалось еще 5 элементов. - person hardwork; 15.05.2015
comment
@D4rWinNS любое решение для этого ... как добавить более 16 элементов в список ..? - person hardwork; 15.05.2015
comment
Я выбрал return Listitemlength;, и его значение равно 21, но это не проблема, проблема в том, что может отображаться только 16 элементов. когда 17-й элемент приходит к первому элементу из массива, не отображая 17-й элемент из массива... - person hardwork; 15.05.2015
comment
Это нормально для вашего кода. В вашем коде вы меняете информацию только в том случае, если позиция равна 15 или меньше, если вы хотите сохранить то, как вы это делаете (это плохой способ), просто добавьте до 21, я отредактировал ответчик - person D4rWiNS; 15.05.2015
comment
я пробую это все, но это не работает. В моем пользовательском списке я хочу создать пользовательскую строку с переключателем после 16-й строки, но этот код не работает, поэтому как добавить этот код для этой строки 17-й и 18-й. Теперь я создал подпоследовательность моего ListView и использую только 16 строк в списке. - person hardwork; 16.05.2015

такая проблема возникает из-за высоты списка. Теперь высота строки списка-представления уменьшена и работает отлично.

person hardwork    schedule 18.05.2015

person    schedule
comment
Пожалуйста, просто не публикуйте свой код. Попробуйте немного объяснить, что делает этот фрагмент кода. - person Anirudh Sharma; 15.05.2015
comment
@Krishna patoliya Ваш код дает исключение errorNullPointer, и у меня нет ничего нулевого .. это указывает мне на это .. convertView = inflater.inflate(R.layout.screenlock_addapter, parent,false); - person hardwork; 15.05.2015
comment
любое другое решение для этого ..?? - person hardwork; 15.05.2015