Android:如何在listview中重复列表项

Android:如何在listview中重复列表项,android,listview,items,Android,Listview,Items,我正在开发一个android应用程序,我必须显示一个包含重复项的列表视图。最初列表视图显示3个项目,但我希望它们显示3到4次。有办法吗 public class CustomAdapter extends ArrayAdapter<HireGroups> { ArrayList<HireGroups> result; Context c; LayoutInflater inflater; public CustomAdapter(Context context, Ar

我正在开发一个android应用程序,我必须显示一个包含重复项的列表视图。最初列表视图显示3个项目,但我希望它们显示3到4次。有办法吗

public class CustomAdapter extends ArrayAdapter<HireGroups> {

ArrayList<HireGroups> result;
Context c;
LayoutInflater inflater;

public CustomAdapter(Context context, ArrayList<HireGroups> list) {
    super(context,0, list);
    c=context;
    result=list;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return result.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean isEnabled(int position) {
    final HireGroups h = result.get(position);
    if(h.getSubHireGroup().size() == 0) {
        return false;
    }
    return super.isEnabled(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;

    final HireGroups i = result.get(position);
    if(i != null) {
        v = inflater.inflate(R.layout.single_hiregroup_item, null);
        final TextView hireGroupName = (TextView) v.findViewById(R.id.hire_group_name);
        final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);
        final TextView desc = (TextView) v.findViewById(R.id.hire_group_desc);
        final ImageView hiregroupImage = (ImageView) v.findViewById(R.id.hire_group_image);
        final TextView vehicleCount = (TextView) v.findViewById(R.id.no_of_vehicles);

        if(hireGroupName != null) {
            hireGroupName.setText(i.getHireGroupName());
        }
        if(subtitle != null) {
            subtitle.setText(i.getSubTitle());
        }
        if(hiregroupImage != null) {
            Picasso.with(c).load(i.getPhotoUrl()).into(hiregroupImage);
        }
        if(desc != null) {
            desc.setText(i.getDescription());
        }
        if(vehicleCount != null)
        {
            int count=i.getSubHireGroup().size();
            Animation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(200); //You can manage the blinking time with this parameter
            anim.setStartOffset(20);
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(Animation.INFINITE);
            if(count > 0)
            {
                vehicleCount.startAnimation(anim);
                vehicleCount.setText("  "+count+" Available ");
            }
            else
                vehicleCount.setText("  "+count+" Available ");
        }

    }
    return v;
}
公共类CustomAdapter扩展了ArrayAdapter{
阵列列表结果;
上下文c;
充气机;
公共CustomAdapter(上下文上下文,ArrayList列表){
超级(上下文,0,列表);
c=上下文;
结果=列表;
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
}
@凌驾
public int getCount(){
返回result.size();
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共布尔值isEnabled(整型位置){
最终雇佣组h=结果。获取(位置);
如果(h.getSubHireGroup().size()==0){
返回false;
}
返回super.isEnabled(位置);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
最终雇佣组i=结果。获取(位置);
如果(i!=null){
v=充气机充气(右布局单组项目,空);
最终文本视图hireGroupName=(文本视图)v.findViewById(R.id.hire\u group\u name);
最终文本视图字幕=(文本视图)v.findViewById(R.id.subtitle);
最终文本视图描述=(文本视图)v.findviewbyd(R.id.hire\u group\u desc);
最终ImageView hiregroupImage=(ImageView)v.findViewById(R.id.hire\u group\u image);
最终文本视图车辆计数=(文本视图)v.findviewbyd(R.id.no\u车辆);
if(hireGroupName!=null){
setText(i.getHireGroupName());
}
如果(副标题!=null){
subtitle.setText(i.getSubTitle());
}
如果(hiregroupImage!=null){
毕加索.with(c).load(i.getPhotoUrl()).into(hiregroupImage);
}
如果(描述!=null){
desc.setText(i.getDescription());
}
if(车辆计数!=null)
{
int count=i.getSubHireGroup().size();
动画动画=新的AlphaAnimation(0.0f,1.0f);
anim.setDuration(200);//您可以使用此参数管理闪烁时间
动画设置开始偏移(20);
动画设置重复模式(动画反转);
anim.setRepeatCount(Animation.INFINITE);
如果(计数>0)
{
车辆计数启动(anim);
vehicleCount.setText(“+count+”可用”);
}
其他的
vehicleCount.setText(“+count+”可用”);
}
}
返回v;
}

}

假设要将同一数据显示3次,则可以执行以下操作

像这样修改getCount()

@Override
public int getCount() {
    return result.size() * 3;
}
并修改getView()


Listview使用的是什么源。ArrayList或Cursor?它是从json字符串填充的类对象的ArrayList。创建扩展
BaseAdapter
和“包装”的自定义适配器原始适配器和override
getCount
返回的值是原始适配器的3到4倍。必须从json字符串中填充arraylist三次,每次只将json对象添加到arraylist。如果有适配器,您可以看到我的编辑。我已经尝试将结果相乘。size()*3,但在检索项目时未应用模数,因此我遇到异常。最初@pskink的回答值得称赞。也谢谢你。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;

    final HireGroups i = result.get(position % 3);
    if(i != null) {
        v = inflater.inflate(R.layout.single_hiregroup_item, null);
        final TextView hireGroupName = (TextView) v.findViewById(R.id.hire_group_name);
        final TextView subtitle = (TextView) v.findViewById(R.id.subtitle);
        final TextView desc = (TextView) v.findViewById(R.id.hire_group_desc);
        final ImageView hiregroupImage = (ImageView) v.findViewById(R.id.hire_group_image);
        final TextView vehicleCount = (TextView) v.findViewById(R.id.no_of_vehicles);

        if(hireGroupName != null) {
            hireGroupName.setText(i.getHireGroupName());
        }
        if(subtitle != null) {
            subtitle.setText(i.getSubTitle());
        }
        if(hiregroupImage != null) {
            Picasso.with(c).load(i.getPhotoUrl()).into(hiregroupImage);
        }
        if(desc != null) {
            desc.setText(i.getDescription());
        }
        if(vehicleCount != null)
        {
            int count=i.getSubHireGroup().size();
            Animation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(200); //You can manage the blinking time with this parameter
            anim.setStartOffset(20);
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(Animation.INFINITE);
            if(count > 0)
            {
                vehicleCount.startAnimation(anim);
                vehicleCount.setText("  "+count+" Available ");
            }
            else
                vehicleCount.setText("  "+count+" Available ");
        }

    }
    return v;
}