2011-01-08 2 views
0

Hallo Ich möchte mehr Bilder in meiner Listenansicht hinzufügen, da dieser Code unten Bild 1 und 2 kontinuierlich in jeder Zeile anzeigt. Ich möchte für jede Zeile ein anderes Bild anzeigen. Hier ist mein Code unten;ein Bild in Listenansicht hinzufügen

Danke für jede Hilfe. Ich bin nicht gut in Java, bitte ändern Sie den Code wo nötig und ich kann mich dann darauf beziehen.

public class Startern erweitert ListActivity { private static Klasse EfficientAdapter erstreckt BaseAdapter { privaten LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private Bitmap mIcon5; private Bitmap mIcon6; private Bitmap mIcon7; private Bitmap mIcon8; private Bitmap mIcon9; private 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"}; 

}

Antwort

1

Was ich tun möchte, ist für jede andere Zeile

Dann legen Sie ein anderes Bild in jeder Reihe ein anderes Bild angezeigt werden soll. Sie haben die Kontrolle darüber, was in jeder Zeile über getView() geht. Ihre aktuelle Implementierung iteriert zwischen zwei verschiedenen Bildern - ändern Sie diese Logik so, wie es für Ihre App funktioniert.

Here is a free excerpt aus einem meiner Bücher, die darauf näher eingehen.