我正在经历一个相当恼人的问题。我在论坛中访问了很多问题解决方案同样的问题,但无法为我工作。我正在使用我创建的适配器加载ListView。我显示的数据来自我的数据库。列表中的每个单元格都有一个单击按钮,调用一个函数来编辑记录数据库中的字段。当listView中的就绪记录正常时,但随着我查看列表视图,某些数据开始发生变化。有一次,我评估我的项目是否已经过验证(是数据库的一个字段,在此验证的情况下可以是0或1)。如果验证,该按钮将加载背景作为图像,并取消denuevo点击它。如果该值已验证为0,则我为此按钮创建了一个单击事件。然后,当我走下去时,listView图片按钮开始改变,即使在不断变化的情况下也会改变,但不同之处在于,如果我可以点击它们。我不明白因为他们在注册表值处于预定值之前进行验证时更改了图像。我会让我的代码。
我的班级适配器
@SuppressLint("InflateParams")
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
private ArrayList<RowItem> items;
LayoutInflater mInflater;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
this.items = new ArrayList<RowItem>();
this.items.addAll(items);
this.mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView txtNombre;
TextView txtTicket;
TextView txtAsiento;
TextView txtOrden;
TextView txtNumero;
TextView txtMensaje;
TextView txtAdicionales;
TextView txtOtros;
TextView txtCategoria;
Button btn;
}
@Override
public int getCount() {
return items.size();
}
@Override
public RowItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.lista_validacion_multiple, null);
holder = new ViewHolder();
holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
holder.txtAsiento = (TextView)convertView.findViewById(R.id.txtAsiento);
holder.txtTicket = (TextView)convertView.findViewById(R.id.txtTicket);
holder.txtNumero = (TextView)convertView.findViewById(R.id.txtNumero);
holder.txtOrden = (TextView)convertView.findViewById(R.id.txtOrden);
holder.txtAdicionales = (TextView)convertView.findViewById(R.id.txtAdicionales);
holder.txtOtros = (TextView)convertView.findViewById(R.id.txtOtros);
holder.txtCategoria = (TextView)convertView.findViewById(R.id.txtCategoria);
holder.btn = (Button)convertView.findViewById(R.id.button1);
holder.txtMensaje = (TextView)convertView.findViewById(R.id.txtMensaje);
convertView.setTag(holder);
} else{
//Whenever recycling Holder, the elements begin to change in the values that I mentioned.
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = getItem(position);
SessionManager manager = new SessionManager();
String codigoEvento = manager.getValue(parent.getContext(), "codigoEvento");
holder.txtOrden.setText(Html.fromHtml("Orden de compra: <b>"+codigoEvento+"-"+rowItem.getId_inscripcion()+"</b>"));
if (!rowItem.getAsiento().equals("") && !rowItem.getAsiento().equals("null") && rowItem.getAsiento() != null) {
holder.txtAsiento.setText(Html.fromHtml("Asiento: <b>"+rowItem.getAsiento()+"</b>"));
holder.txtAsiento.setVisibility(View.VISIBLE);
}
if (!rowItem.getNumero().equals("") && !rowItem.getNumero().equals("null") && rowItem.getNumero() != null) {
holder.txtNumero.setText(Html.fromHtml("Número: <b>"+rowItem.getNumero()+"</b>"));
holder.txtNumero.setVisibility(View.VISIBLE);
}
if (!rowItem.getAdicionales().equals("") && !rowItem.getAdicionales().equals("null") && rowItem.getAdicionales() != null) {
holder.txtAdicionales.setText(Html.fromHtml("Adicionales: <b>"+rowItem.getAdicionales()+"</b>"));
holder.txtAdicionales.setVisibility(View.VISIBLE);
}
if (!rowItem.getOtros().equals("") && !rowItem.getOtros().equals("null") && rowItem.getOtros() != null) {
holder.txtOtros.setText(Html.fromHtml("Otros: <b>"+rowItem.getOtros()+"</b>"));
holder.txtOtros.setVisibility(View.VISIBLE);
}
if(rowItem.getCategoria() != null){
if (!rowItem.getCategoria().equals("") && !rowItem.getCategoria().equals("null") && rowItem.getCategoria() != null || !rowItem.getCategoria().isEmpty()) {
holder.txtCategoria.setText(Html.fromHtml("Categoría: <b>"+rowItem.getCategoria()+"</b>"));
holder.txtCategoria.setVisibility(View.VISIBLE);
}
}
//Adicionales y otros
if(rowItem.getValidado()==1){
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
holder.txtMensaje.setText("E-ticket ya validado");
holder.txtMensaje.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.btn.setLayoutParams(params);
}else{
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout layout=(RelativeLayout) v.getParent();
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Button b = (Button)v.findViewById(R.id.button1);
TextView t=(TextView)layout.findViewById(R.id.txtMensaje);
t.setText("E-ticket validado");
Activity activity = (Activity)CustomListViewAdapter.this.getContext();
TextView ts = (TextView)(activity.findViewById(R.id.txtDisponibles));
t.setText("E-ticket validado");
t.setVisibility(View.VISIBLE);
b.setBackgroundResource(R.drawable.icon_big_check);
b.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(params);
SessionManager manager = new SessionManager();
BaseDeDatos nueva = new BaseDeDatos();
nueva.validarMultiple(activity, item_click);
int validadas = manager.getValueInt(parent.getContext(), "validadas");
int totales = manager.getValueInt(parent.getContext(), "totales");
if(validadas > 0){
validadas = validadas - 1;
manager.setValueInt(parent.getContext(), "validadas", validadas);
manager.setValueInt(parent.getContext(), "totales", totales);
ts.setText(validadas+"/"+totales);
}
}
});
}
return convertView;
}
}
布局列表:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtNombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:text="TextView" />
<TextView
android:id="@+id/txtTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtNombre"
android:paddingLeft="10dp"
android:text="TextView" />
<TextView
android:id="@+id/txtOrden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTicket"
android:paddingLeft="10dp"
android:text="" />
<TextView
android:id="@+id/txtAsiento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtOrden"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<TextView
android:id="@+id/txtNumero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtAsiento"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<TextView
android:id="@+id/txtAdicionales"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtNumero"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<TextView
android:id="@+id/txtOtros"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtAdicionales"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<TextView
android:id="@+id/txtCategoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtOtros"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtOrden"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:background="@drawable/btn_green_small"
android:maxHeight="48dp"
android:maxWidth="80dp"
android:shadowColor="#A8A8A8"
android:text="Validar"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txtMensaje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="SDSDSDS"
android:layout_marginRight="18dp"
android:visibility="gone"
android:textSize="10dp" />
</RelativeLayout>
答案 0 :(得分:1)
这是因为视图回收。当适配器需要在列表中显示新元素时有两种可能:
因此,您需要将正确的值放在getView中的list元素中。如果rowItem.getValidado()!=1
您将OnClickListener
设置为按钮但未更改正确按钮的背景,则不会使用按钮执行此操作。
您可以在RowItem
内添加字段以保存状态并恢复getView
内的字段状态。像这样:
if(rowItem.getClicked()){
holder.btn.setBackgroundResource(R.drawable.icon_big_check);
holder.btn.setOnClickListener(null);
}else if(rowItem.getValidado()==1){
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
holder.txtMensaje.setText("E-ticket ya validado");
holder.txtMensaje.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.btn.setLayoutParams(params);
holder.btn.setOnClickListener(null);
}else{
holder.btn.setBackgroundResource(R.drawable.btn_green_small);
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rowItem.setClicked(true);
RelativeLayout layout=(RelativeLayout) v.getParent();
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Button b = (Button)v.findViewById(R.id.button1);
TextView t=(TextView)layout.findViewById(R.id.txtMensaje);
t.setText("E-ticket validado");
Activity activity = (Activity)CustomListViewAdapter.this.getContext();
TextView ts = (TextView)(activity.findViewById(R.id.txtDisponibles));
t.setText("E-ticket validado");
t.setVisibility(View.VISIBLE);
b.setBackgroundResource(R.drawable.icon_big_check);
b.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(params);
SessionManager manager = new SessionManager();
BaseDeDatos nueva = new BaseDeDatos();
nueva.validarMultiple(activity, item_click);
int validadas = manager.getValueInt(parent.getContext(), "validadas");
int totales = manager.getValueInt(parent.getContext(), "totales");
if(validadas > 0){
validadas = validadas - 1;
manager.setValueInt(parent.getContext(), "validadas", validadas);
manager.setValueInt(parent.getContext(), "totales", totales);
ts.setText(validadas+"/"+totales);
}
}
});
}