我正在开发一个巴士预订应用程序。我使用Recyclerview和GridLayoutManager创建了座位预订布局。布局正确创建。我有1排4个座位,最后一排有5个座位。所以我在GridLayoutManager中使用了5列。中间列将为2个座位之间的空间用于行走。现在问题是如果我点击第一个座位将选择1s座位但是第5个座位的图像更改。而且我还在textview中设置了座位号,所以有时它显示正确的座位号,有时它显示空白或有时错误。
SeatSelection活动
public class SeatSelection extends AppCompatActivity implements AdapterView.OnItemClickListener{
RecyclerView recyclerView;
SeatAdapter seatAdapter;
List<SeatDetails> seatDetailsList;
TextView txtSeatNo;
ImageView imageView;
TextView fare;
public Bitmap seatIcon;
public Bitmap seatSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat_selection);
seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_avl);
seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_std);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 5);
recyclerView.setLayoutManager(layoutManager);
seatAdapter = new SeatAdapter(this, loadBustListSeats());
recyclerView.setAdapter(seatAdapter);
}
public class SeatDetails {
String name;
boolean isBlank;
boolean isSelected;
public SeatDetails(String name, boolean isBlank, boolean isSelected)
{
this.name = name;
this.isBlank = isBlank;
this.isSelected = isSelected;
}
public String getName() {
return name;
}
}
public List<SeatDetails> loadBustListSeats() {
seatDetailsList = new ArrayList<SeatDetails>();
int seatSerialNumber = 1;
for (int i = 0; i <= 60; i++) {
if (isBlankSeat(i)) {
seatDetailsList.add(new SeatDetails("", true, false));
} else {
if (i == 0) {
seatDetailsList.add(new SeatDetails("A", true, false));
} else if (i == 1) {
seatDetailsList.add(new SeatDetails("B", true, false));
} else {
seatDetailsList.add(new SeatDetails(String.valueOf(seatSerialNumber), true, false));
seatSerialNumber = seatSerialNumber + 1;
}
}
}
return seatDetailsList;
}
public boolean isBlankSeat(int i) {
if ((60 - (i - 1)) < 5)
return false;
else
return i % 5 == 2 ;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
public void seatSelected(int pos)
{
charge = charge + amt;
imageView.setImageBitmap(seatSelect);
txtSeatNo.setText(seatDetailsList.get(pos).getName());
fare.setText(String.valueOf(charge));
seatDetailsList.remove(pos);
seatDetailsList.add(pos, new SeatDetails(seatDetailsList.get(pos).getName(), true, true));
seatAdapter.notifyDataSetChanged();
}
public void seatDeselected(int pos)
{
charge = charge - amt;
imageView.setImageBitmap(seatIcon);
fare.setText(String.valueOf(charge));
seatDetailsList.remove(pos);
seatDetailsList.add(pos, new SeatDetails(seatDetailsList.get(pos).getName(), true, false));
seatAdapter.notifyDataSetChanged();
}
public class SeatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
class ViewHolderOriginal extends RecyclerView.ViewHolder{
TextView textView;
ViewHolderOriginal(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.item_image);
textView = (TextView) itemView.findViewById(R.id.item_text);
}
}
class ViewHolderBlank extends RecyclerView.ViewHolder{
ViewHolderBlank(View itemView) {
super(itemView);
}
}
private Context cntx;
public SeatAdapter(Context context, List<SeatDetails> list){
cntx = context;
seatDetailsList = list;
}
// 0= Original view 1= Blank View
@Override
public int getItemViewType(int position) {
if((getItemCount()-(position-1))<5)
return 0;
else
return position % 5 == 2 ? 1 : 0;
}
@Override
public int getItemCount() {
return 60;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (viewType == 1)
return new ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
else
return new ViewHolderOriginal(inflater.inflate(R.layout.seatrow_grid, parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderBlank) {
} else if (holder instanceof ViewHolderOriginal) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
seatDeselected(position);
}
else
{
seatSelected(position);
}
}
});
}
}
}
}
答案 0 :(得分:0)
您的代码有多个问题需要修复和改进
让我们查看此代码以选择座位:
public void seatSelected(int pos)
{
charge = charge + amt;
imageView.setImageBitmap(seatSelect);
txtSeatNo.setText(seatDetailsList.get(pos).getName());
fare.setText(String.valueOf(charge));
seatDetailsList.remove(pos);
seatDetailsList.add(pos, new SeatDetails(seatDetailsList.get(pos).getName(), true, true));
seatAdapter.notifyDataSetChanged();
}
假设您的列表为[0, 1, 2, 3, 4]
,并且您要在pos = 3
添加项目。
首先,您致电:seatDetailsList.remove(pos);
- &gt;您的列表将变为[0, 1, 2, 4]
。
然后你正在打电话
seatDetailsList.add(pos, new SeatDetails(seatDetailsList.get(pos).getName(), true, true));
在此方法调用中,您从列表中获取seatDetailsList.get(pos).getName()
,列表中的当前pos = 3
项目为4
您最终会添加一个数字为{{{1}的席位1}}到这样的列表:
4
不是[0, 1, 2, 4, 4]
正如您所料。
然后想象一下你选择列表中的最后一个席位。如果删除最后一个索引([0, 1, 2, 3, 4]
),则获取4
的权限将引发异常,因为您正在访问列表外的内存(seatDetailsList.get(pos)
)。
所以你可以通过修改座位的当前状态来解决这个问题,而不是删除并重新添加它们。
试试这个:
ArrayOutOfBoundIndexException
尝试使用public void seatSelected(int pos)
{
charge = charge + amt;
imageView.setImageBitmap(seatSelect);
txtSeatNo.setText(seatDetailsList.get(pos).getName());
fare.setText(String.valueOf(charge));
//seatDetailsList.remove(pos);
//seatDetailsList.add(pos, new SeatDetails(seatDetailsList.get(pos).getName(), true, true));
seatDetailList.get(pos).setIsSelected(true);
seatAdapter.notifyDataSetChanged();
}
函数执行相同的操作,并将循环更改为seatDeselected
修改
for (int i = 0; i < 60; i++)
在你的听众中:
class ViewHolderOriginal extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView; // Add this as internal property
ViewHolderOriginal(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.item_image);
textView = (TextView) itemView.findViewById(R.id.item_text);
}
}