2016-08-17 72 views
1

我有一個列表視圖,我打算通過我的適配器添加一些圖像到每一行;當我運行我的程序時,它不會給出任何錯誤,但它會向列表視圖添加錯誤的圖像數量,並且當我滾動列表視圖時,圖像視圖的數量也會發生變化! 我的適配器類如下所示:動態添加圖像到列表視圖

public class RideAdapter extends ArrayAdapter { 
List list = new ArrayList(); 
public RideAdapter(Context context, int resource){ 
    super(context, resource); 
} 

public void add(Rides object) { 
    super.add(object); 
    list.add(object); 
} 

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

@Override 
public Object getItem(int position) { 
    return list.get(position); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row; 
    row = convertView; 
    RideHolder rideHolder; 
    if(row == null){ 

     LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = layoutInflater.inflate(R.layout.list_row, parent, false); 
     rideHolder = new RideHolder(); 
     rideHolder.tx_start = (TextView)row.findViewById(R.id.tx_start); 
     rideHolder.tx_end = (TextView)row.findViewById(R.id.tx_end); 
     rideHolder.tx_seats = (TextView)row.findViewById(R.id.tx_seats); 
     rideHolder.tx_distance = (TextView)row.findViewById(R.id.tx_distance); 
     rideHolder.tx_name = (TextView)row.findViewById(R.id.tx_name); 
     rideHolder.linearLayout_seats = (LinearLayout) row.findViewById(R.id.linearLayout_seats); 
     row.setTag(rideHolder); 
    } 
    else { 
     rideHolder = (RideHolder) row.getTag(); 
    } 
    Rides ride = (Rides) this.getItem(position); 
    rideHolder.tx_start.setText(ride.getStart()); 
    rideHolder.tx_end.setText(ride.getEnd()); 
    rideHolder.tx_name.setText(ride.getName()); 

    for(int i = 0; i < ride.getSeats(); i++) { 
     ImageView image = new ImageView(this.getContext()); 
     image.setImageResource(R.drawable.empty_seat); 
     rideHolder.linearLayout_seats.addView(image); 
    } 
    return row; 
} 

static class RideHolder{ 
    TextView tx_start, tx_end, tx_seats, tx_distance, tx_name; 
    LinearLayout linearLayout_seats; 
} 

任何人都可以幫助我嗎?

謝謝

+0

請用recyclerview或畢加索庫 –

+0

請告訴我什麼是如果圖像是不是從互聯網(URL)來設置爲 – Alikbar

+0

列表視圖的高度和寬度,但您可繪製文件夾,你需要像這樣做--- rideHolder.TheImageView.setImageResource(TheContext.getResources()。getIdentifier(ArrayDrawableName,「drawable」,TheContext.getPackageName())); - 所有你需要的是一個ImageView的持有者,併爲你的數組中的每個項目添加可繪製的名稱 – Tasos

回答

2

只需添加視圖,從佈局

Rides ride = (Rides) this.getItem(position); 
rideHolder.tx_start.setText(ride.getStart()); 
rideHolder.tx_end.setText(ride.getEnd()); 
rideHolder.tx_name.setText(ride.getName()); 

rideHolder.linearLayout_seats.removeAllViews(); 

for(int i = 0; i < ride.getSeats(); i++) { 
    ImageView image = new ImageView(this.getContext()); 
    image.setImageResource(R.drawable.empty_seat); 
    rideHolder.linearLayout_seats.addView(image); 
} 
return row; 
+0

如果你覺得有用接受答案 – Don

+0

非常感謝你;) –

0

佈局removeAllViews之前將它添加到佈局之前設置setLayoutParams

for(int i = 0; i < ride.getSeats(); i++) { 

    //ImageView Setup 
    ImageView imageView = new ImageView(this); 
    //ImageView imageView = new ImageView(this.getContext()); 

    //setting image resource 
    imageView.setImageResource(R.drawable.empty_seat); 

    //setting image position 
    imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
    LayoutParams.WRAP_CONTENT)); 

    //adding view to layout 
    rideHolder.linearLayout_seats.addView(image); 
}