2016-03-11 3 views
0

Я разрабатываю приложение для Android, я должен отображать представление списка с повторяющимися элементами. Первоначально в представлении списка отображается 3 элемента, но я хочу, чтобы они отображались 3 или 4 раза. Есть ли способ сделать это?Android: как повторить элементы списка в списке

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

}

+0

Какой источник используется в вашем Listview. ArrayList или курсор? –

+0

Это arraylist объектов класса, который заполняется из строки json. –

+0

создать пользовательский адаптер, который расширяет «BaseAdapter» и «обертывает» оригинал, и переопределяет 'getCount', возвращающийся в 3 или 4 раза больше, чем ваш исходный адаптер делает – pskink

ответ

0

Предположим, что вы хотите отобразить те же данные в 3 раза, то, что вы можете сделать, это

изменить GetCount() как этот

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

и изменить getView()

@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; 
} 
+0

Я уже пробовал умножить result.size() * 3, но не применял модуль во время извлечения элемента, поэтому я получал исключение. Первоначально @pskink заслуживает заслуги в этом ответе. И тебе спасибо. –

Смежные вопросы