gpt4 book ai didi

android - 自定义ListView滚动重复数据

转载 作者:行者123 更新时间:2023-11-29 01:23:03 26 4
gpt4 key购买 nike

我正在经历一个相当烦人的问题。我在论坛中访问过许多针对同一问题的问题解决方案,但无法为我工作。我正在使用我创建的适配器加载 ListView。我显示的数据来 self 的数据库。列表中的每个单元格都有一个按钮可以单击,调用一个函数来编辑数据库中记录的字段。当此 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>

最佳答案

这是因为 View 回收。当适配器需要在您的列表中显示一个新元素时有两种可能性:

  1. 创建一个新元素:此元素具有布局的默认值。
  2. 重用不再可见的元素:此元素具有旧值。

因此,您需要在 getView 中将正确的值放入您的列表元素中。如果 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);
}
}
});
}

关于android - 自定义ListView滚动重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com