如何在GetView中使用holder来配置包含16个以上项目的Listview

时间:2015-05-15 05:34:50

标签: android baseadapter android-viewholder getview

我在使用BaseAdapter在ListView中添加项目时遇到了getView的问题。当我将“0到15项”存储为“if(position == 0 )if(position == 15)”时,它会依次显示所有ListView项目。当我尝试将另外一个(第16个)项添加到ListView(如此if(position == 16))时,它会显示ListView中的第一个项目,并且不显示我已添加到数组中的第16个项目。

我正在使用自定义ListView行,每行都有自己的与用户交互的功能。但我不知道如何使用'position'在getView中添加超过16个项目。

我有21项的列表视图,但我不能实现超过16项。

任何人都有这个解决方案,请告诉我

GetView代码

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

4 个答案:

答案 0 :(得分:0)

试试这段代码......

public View getView(int position,View convertView,ViewGroup parent){     // TODO自动生成的方法存根     持有人;     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;

}

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

如果您希望保持这种方式,您只需更改位置为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);
        }

...等

危险:这是一个非常糟糕的做法,你不应该这样做!你应该只做一次工作

答案 3 :(得分:0)

这种问题是由于列表视图的高度而发生的。现在列表视图行的高度减少了,它的工作也很完美。