gpt4 book ai didi

android - 我无法通过单击 imageButtom 删除带有自定义适配器的 ListView 中的正确项目

转载 作者:行者123 更新时间:2023-11-29 20:54:28 25 4
gpt4 key购买 nike

当我点击 listView 中的 imageButtom 时,它总是删除第一个项目。我需要删除它的 imageButtom 被点击的项目。

PedidoLista.class

public class PedidoLista extends BaseAdapter {
private LayoutInflater mInflater;
private List<Movimiento> productos;

public PedidoLista(Context context, List<Movimiento> productos) {
this.productos = productos;
mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return productos.size();
}

@Override
public Object getItem(int position) {
return productos.get(position);
}

@Override
public long getItemId(int position) {
return (long) Integer.parseInt(productos.get(position).getIdref());
}

@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
ViewHolder holder;
View view;
if (convertView == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.pedido_lista, null);
holder.txt1 = (TextView) view.findViewById(R.id.lista1);
holder.txt2 = (TextView) view.findViewById(R.id.lista2);
holder.txt3 = (TextView) view.findViewById(R.id.lista3);
holder.ib4 = (ImageButton) view.findViewById(R.id.lista4);
holder.ib4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
productos.remove(position);
notifyDataSetChanged();
}
});
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Movimiento papa = this.productos.get(position);
holder.txt1.setText(papa.getNombrepale());
holder.txt2.setText(papa.getDescripLarga());
holder.txt3.setText(papa.getCant());
return view;
}
public static class ViewHolder {
TextView txt1;
TextView txt2;
TextView txt3;
ImageButton ib4;
}
}

pedo_lista.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/asd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/lista1"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:lines="1"
android:maxLength="4"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="horizontal">

<TextView
android:id="@+id/lista2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:maxLength="35"
android:text="123456789123456789123456789123456789"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="horizontal">

<TextView
android:id="@+id/lista3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="8"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:gravity="center"
android:orientation="horizontal">

<ImageButton
android:id="@+id/lista4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/kkkrojo"
android:src="@android:drawable/ic_menu_delete" />
</LinearLayout>

我需要这方面的帮助!

最佳答案

尝试类似下面的代码。但我想您应该使用 RecyclerView。已经实现了。

public class PedidoLista extends BaseAdapter {
private LayoutInflater mInflater;
private List<Movimiento> productos;

public PedidoLista(Context context, List<Movimiento> productos) {
this.productos = productos;
mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return productos.size();
}

@Override
public Object getItem(int position) {
return productos.get(position);
}

@Override
public long getItemId(int position) {
return (long) Integer.parseInt(productos.get(position).getIdref());
}

@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
//you have to make your view holder as final
final ViewHolder holder;
View view;
if (convertView == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.pedido_lista, null);
holder.txt1 = (TextView) view.findViewById(R.id.lista1);
holder.txt2 = (TextView) view.findViewById(R.id.lista2);
holder.txt3 = (TextView) view.findViewById(R.id.lista3);
holder.ib4 = (ImageButton) view.findViewById(R.id.lista4);
holder.ib4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here you get current position from view holder
productos.remove(holder.position);
notifyDataSetChanged();
}
});
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Movimiento papa = this.productos.get(position);
//here you set current position
holder.position = position;
holder.txt1.setText(papa.getNombrepale());
holder.txt2.setText(papa.getDescripLarga());
holder.txt3.setText(papa.getCant());
return view;
}
public static class ViewHolder {
TextView txt1;
TextView txt2;
TextView txt3;
ImageButton ib4;
int position;
}
}

关于android - 我无法通过单击 imageButtom 删除带有自定义适配器的 ListView 中的正确项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28174457/

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