1

I have a problem with getView with adding items in ListView with BaseAdapter. When I store "0 to 15 items " like this " if(position == 0 ) to if(position == 15) " it displays all the ListView items in sequence. When I try adding one more (16th) item to the ListView (like this if(position == 16)), it displays the first item in the ListView and doesn't show the 16th item which I have added to the array.

I am using a custom ListView row and each row has its own function which interacts with users. But I don't know how to add more than 16 items in getView using 'position'.

I have listview of 21 items but i can not implement its more than 16 items.

Any one have solution of this than pls tell me

GetView Code

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;
}
hardwork
  • 781
  • 7
  • 14

4 Answers4

0

Try out this code...

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

}

Rohit
  • 2,646
  • 6
  • 27
  • 52
  • Not, Its still not working, its showing only 16 items (0 to 15). on 17th item it display first item.. – hardwork May 15 '15 at 05:51
  • first item should start with position of listview "0" but with your code its start position of listviw with "1". it display "0 " item on position 17th – hardwork May 15 '15 at 05:59
  • if another solution is available than pls write. – hardwork May 15 '15 at 06:12
  • By using your code , when i go to last row of listview and comback to first .. it change the position of row and display view in non sequence. – hardwork May 15 '15 at 06:57
0
 public View getView(int position, View convertView, ViewGroup parent){
 MyViewHolder mViewHolder;

     if(convertView == null) {
     convertView = inflater.inflate(R.layout.screenlock_addapter, parent,false);
     mViewHolder = new MyViewHolder();
     convertView.setTag(mViewHolder);
     } else {
     mViewHolder = (MyViewHolder) convertView.getTag();
     }

     mViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.scl_tv);
     mViewHolder.tvDesc  = (TextView)convertView.findViewById(R.id.scl_desc);
      mViewHolder.ivIcon =(ImageView)convertView.findViewById(R.id.scl_img);

         mViewHolder.tvTitle.setText(Listitem[position]);
         mViewHolder.tvDesc.setText(desc1[position]);
         mViewHolder.ivIcon.setImageResource(aimg[position]);
         return convertView;
    }

    private class MyViewHolder {
         TextView tvTitle, tvDesc;
         ImageView ivIcon;
    }
krishna
  • 301
  • 1
  • 8
  • Please just do not post your code.Try to explain a little as to what this piece of code does. – Anirudh Sharma May 15 '15 at 06:36
  • @Krishna patoliya Your code is giving error"NullPointer ecxeption" and i have not anithing is null.. its pointing me on this .. `convertView = inflater.inflate(R.layout.screenlock_addapter, parent,false);` – hardwork May 15 '15 at 06:49
  • any other solution for this ..?? – hardwork May 15 '15 at 06:59
0

You only change the information if the position is 15 or less if you want to keep that way you should implement this :

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

...etc

DANGER: THIS IS A VERY BAD PRACTICE, YOU SHOULDN'T DO THIS! YOU SHOULD DO THE WORK ONLY 1 TIME

D4rWiNS
  • 2,585
  • 5
  • 32
  • 54
  • No i wont display 21 items in my listview.. 16 items already displayed but i want more 5 items to display after that 16 items. – hardwork May 15 '15 at 06:52
  • @D4rWinNS any solution for this... how to add more than 16 items in listview ..? – hardwork May 15 '15 at 06:53
  • i have tacken ther `return Listitemlength;` and is value is 21 but it is not problem problem is only 16 items can display. when 17th item come its tack first item from array not displaying 17th item from array ... – hardwork May 15 '15 at 07:19
  • That is normal with your code, In your code, you are only changing the information if the position is 15 or less, if you want to keep the way you are doing it ( it is bad way) just add until 21, I edited the anwser – D4rWiNS May 15 '15 at 10:43
  • i try this all but it is not working. In my custom listview i want to create custom row with switch after 16th row but this code is not working so how can add that code for this row 17th and 18th. Now i have created sub sequence of my ListView and i am using only 16 rows in listview. – hardwork May 16 '15 at 13:52
0

this kind of problem occurs due height of list-view. Now the height of list-view's row is reduced and its work perfectly.

hardwork
  • 781
  • 7
  • 14