2011-01-08 5 views
0

Привет Я хотел бы добавить больше изображений в мое представление списка, так как этот код ниже отображает только изображения 1 и 2 в каждой строке. Я хочу показать другое изображение для каждой отдельной строки. Ниже приведен код mycode;Добавить изображение в listview

Спасибо за любую помощь. Я плохо разбираюсь в java, пожалуйста, измените код там, где это необходимо, и я могу обратиться к нему.

стартеры общественного класса распространяется ListActivity { частный статический класс EfficientAdapter расширяет BaseAdapter { частное LayoutInflater mInflater; частный Bitmap mIcon1; частный Bitmap mIcon2; частный Bitmap mIcon3; частный Bitmap mIcon4; частный Bitmap mIcon5; частный Bitmap mIcon6; частный Bitmap mIcon7; частный Bitmap mIcon8; private Bitmap mIcon9; частный Bitmap mIcon10;

public EfficientAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 

     // Icons bound to the rows. 
     mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters1); 
     mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters2); 
     mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters3); 
     mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters4); 
     mIcon5 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters5); 
     mIcon6 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters6); 
     mIcon7 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters7); 
     mIcon8 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters8); 
     mIcon9 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters9); 
     mIcon10 = BitmapFactory.decodeResource(context.getResources(), R.drawable.starters10); 
    } 

    public int getCount() { 
     return DATA.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is no need 
     // to reinflate it. We only inflate a new View when the convertView supplied 
     // by ListView is null. 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.starters, null); 

      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 
      holder = new ViewHolder(); 

      holder.text = (TextView) convertView.findViewById(R.id.text01); 
      holder.text = (TextView) convertView.findViewById(R.id.secondLine); 
      holder.icon = (ImageView) convertView.findViewById(R.id.icon01); 

      convertView.setTag(holder); 
     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     holder.text.setText(DATA[position]); 
     holder.icon.setImageBitmap((position & 1) ==1 ? mIcon1 : mIcon2); 


     return convertView; 
    } 

    static class ViewHolder { 
     TextView text; 
     ImageView icon; 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setListAdapter(new EfficientAdapter(this)); 
} 

private static final String[] DATA = { 
    "Original nachos", "Toasted chicken and cheese quesadillas", "Chicken, lime and coriander nachos", 
    "Spicy bean and cheese quesadillas", "Tuna and corn quesadillas", "Cheesy bean and sweetcorn nachos", "Crispy chicken, avocado and lime salad", "Beef and baby corn tostada", 
    "Spicy mexican rice with chicken and prawns", "Chilli potato boats"}; 

}

ответ

1

То, что я хочу сделать, это отображать различные изображения для каждого отдельного ряда

Затем положить другое изображение в каждой строке. У вас есть контроль над тем, что идет в каждой строке, через getView(). Ваша текущая реализация выполняет итерацию между двумя разными изображениями - измените эту логику на то, что работает для вашего приложения.

Here is a free excerpt из одной из моих книг, которая более подробно описывает это.

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