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;
}
}
pedido_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>
我需要帮助!
答案 0 :(得分:2)
尝试类似下面的代码。但我想你应该使用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;
}
}
答案 1 :(得分:0)
以下代码应设置在if-else:
之外 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();
}
});