我有一個列表視圖,我打算通過我的適配器添加一些圖像到每一行;當我運行我的程序時,它不會給出任何錯誤,但它會向列表視圖添加錯誤的圖像數量,並且當我滾動列表視圖時,圖像視圖的數量也會發生變化! 我的適配器類如下所示:動態添加圖像到列表視圖
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;
}
任何人都可以幫助我嗎?
謝謝
請用recyclerview或畢加索庫 –
請告訴我什麼是如果圖像是不是從互聯網(URL)來設置爲 – Alikbar
列表視圖的高度和寬度,但您可繪製文件夾,你需要像這樣做--- rideHolder.TheImageView.setImageResource(TheContext.getResources()。getIdentifier(ArrayDrawableName,「drawable」,TheContext.getPackageName())); - 所有你需要的是一個ImageView的持有者,併爲你的數組中的每個項目添加可繪製的名稱 – Tasos