我在ListView中遇到问题。有19个项目要显示。每行的NumMesa值从1到19。 但是在列表中,第1到13行显示正确,但是在第13行之后,这些行从1重复到6。
这是我的代码,可能是您找到原因了
public class MyAppAdapter extends BaseAdapter //has a class viewholder which holds
{
public class ViewHolder {
TextView textName;
TextView txtEstado;
ImageView imageView;
LinearLayout linear;
}
public List<ListaMesas> parkingList;
public Context context;
ArrayList<ListaMesas> arraylist;
private MyAppAdapter(List<ListaMesas> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ListaMesas>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content_mesas, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
viewHolder.txtEstado = (TextView) rowView.findViewById(R.id.txtEstado);
viewHolder.linear = (LinearLayout) rowView.findViewById(R.id.linear);
viewHolder.textName.setText(parkingList.get(position).nummesa);
String numMesa = parkingList.get(position).nummesa;
Log.d("mesa","mesa ="+parkingList.get(position).nummesa);
Log.d("prueba","prueba numMesa todas las mesas "+parkingList.get(position).nummesa);
if (numMesa != null && !numMesa .isEmpty() && !numMesa.equals("null")) { //aqui
viewHolder.textName.setText(parkingList.get(position).nummesa);
numMesa = parkingList.get(position).nummesa;
if (numMesa != null && !numMesa.isEmpty() && !numMesa.equals("null")) {
Log.d("prueba", "prueba numMesa todas las mesas comparadas " + parkingList.get(position).nummesa);
Log.d("mesa","mesa foos size"+foos.size());
for (int i = 0; i < foos.size(); i++) {
Log.d("prueba", "prueba numMesa todas las mesas comparadas " + parkingList.get(position).nummesa);
String orden = foos.get(i).nummesa;
String icono = foos.get(i).estado_mesa;
String id = foos.get(i).idTicket;
String numTicket = foos.get(i).numeroticket;
//String nuTicket = foos.get(i).numTicket;
Log.d("mesa","mesa orden mesa="+i);
Log.d("mesa","mesa numesa="+orden);
Log.d("mesa","mesa estadoMesa="+icono);
Log.d("mesa","mesa id ticket="+id);
Log.d("mesa","mesa numeroticket="+numTicket);
if (numMesa.equals(orden)){
if (icono.equals("100")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro MESA OCUPADA" );
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
viewHolder.textName.setText(parkingList.get(position).nummesa);
viewHolder.linear.setBackgroundColor(Color.GREEN);
viewHolder.txtEstado.setText(icono);
}
if (icono.equals("200")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro MESA EN LOS POSTRES" );
viewHolder.linear.setBackgroundColor(Color.BLUE);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("300")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro TICKET IMPRESO" );
viewHolder.linear.setBackgroundColor(Color.MAGENTA);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("400")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro TICKET IMPRESO" );
viewHolder.linear.setBackgroundColor(Color.RED);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
}
}
}
}
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// here setting up names and images
return rowView;
}
}
}
答案 0 :(得分:0)
您想念setTag模式。仅解决视图,但您每次都需要更新。 请像这样更新您的代码
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content_mesas, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
viewHolder.txtEstado = (TextView) rowView.findViewById(R.id.txtEstado);
viewHolder.linear = (LinearLayout) rowView.findViewById(R.id.linear);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textName.setText(parkingList.get(position).nummesa);
String numMesa = parkingList.get(position).nummesa;
Log.d("mesa","mesa ="+parkingList.get(position).nummesa);
Log.d("prueba","prueba numMesa todas las mesas "+parkingList.get(position).nummesa);
if (numMesa != null && !numMesa .isEmpty() && !numMesa.equals("null")) { //aqui
viewHolder.textName.setText(parkingList.get(position).nummesa);
numMesa = parkingList.get(position).nummesa;
if (numMesa != null && !numMesa.isEmpty() && !numMesa.equals("null")) {
Log.d("prueba", "prueba numMesa todas las mesas comparadas " + parkingList.get(position).nummesa);
Log.d("mesa","mesa foos size"+foos.size());
for (int i = 0; i < foos.size(); i++) {
Log.d("prueba", "prueba numMesa todas las mesas comparadas " + parkingList.get(position).nummesa);
String orden = foos.get(i).nummesa;
String icono = foos.get(i).estado_mesa;
String id = foos.get(i).idTicket;
String numTicket = foos.get(i).numeroticket;
//String nuTicket = foos.get(i).numTicket;
Log.d("mesa","mesa orden mesa="+i);
Log.d("mesa","mesa numesa="+orden);
Log.d("mesa","mesa estadoMesa="+icono);
Log.d("mesa","mesa id ticket="+id);
Log.d("mesa","mesa numeroticket="+numTicket);
if (numMesa.equals(orden)){
if (icono.equals("100")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro MESA OCUPADA" );
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
viewHolder.textName.setText(parkingList.get(position).nummesa);
viewHolder.linear.setBackgroundColor(Color.GREEN);
viewHolder.txtEstado.setText(icono);
}
if (icono.equals("200")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro MESA EN LOS POSTRES" );
viewHolder.linear.setBackgroundColor(Color.BLUE);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("300")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro TICKET IMPRESO" );
viewHolder.linear.setBackgroundColor(Color.MAGENTA);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("400")){
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro " + numMesa+" "+orden);
Log.d("prueba", "prueba numMesa todas las mesas comparadas dentro TICKET IMPRESO" );
viewHolder.linear.setBackgroundColor(Color.RED);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
}
}
}
}
// here setting up names and images
return rowView;
}
}
答案 1 :(得分:0)
按以下步骤修改代码。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content_mesas, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
viewHolder.txtEstado = (TextView) rowView.findViewById(R.id.txtEstado);
viewHolder.linear = (LinearLayout) rowView.findViewById(R.id.linear);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textName.setText(parkingList.get(position).nummesa);
String numMesa = parkingList.get(position).nummesa;
if (numMesa != null && !numMesa.isEmpty() && !numMesa.equals("null")) { //aqui
viewHolder.textName.setText(parkingList.get(position).nummesa);
numMesa = parkingList.get(position).nummesa;
if (numMesa != null && !numMesa.isEmpty() && !numMesa.equals("null")) {
for (int i = 0; i < foos.size(); i++) {
String orden = foos.get(i).nummesa;
String icono = foos.get(i).estado_mesa;
String id = foos.get(i).idTicket;
String numTicket = foos.get(i).numeroticket;
if (numMesa.equals(orden)) {
if (icono.equals("100")) {
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
viewHolder.textName.setText(parkingList.get(position).nummesa);
viewHolder.linear.setBackgroundColor(Color.GREEN);
viewHolder.txtEstado.setText(icono);
}
if (icono.equals("200")) {
viewHolder.linear.setBackgroundColor(Color.BLUE);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("300")) {
viewHolder.linear.setBackgroundColor(Color.MAGENTA);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
if (icono.equals("400")) {
viewHolder.linear.setBackgroundColor(Color.RED);
viewHolder.textName.setTextColor(Color.WHITE);
viewHolder.txtEstado.setText(icono);
parkingList.get(position).estadomesa = icono;
parkingList.get(position).idTicket = id;
parkingList.get(position).numTicket = numTicket;
}
}
}
}
}
return rowView;
}
问题-您的代码问题是if(rowView==null)
仅在该位置需要新视图时才会被调用。当您将隐式视图作为参数时,它将不会被调用。这就是ViewHolder
模式在适配器中的工作方式。.
PS:-移至RecyclerView
。 ListView
已成为旧版。