Custom adapter

-3

hi what I hope you can help me I have an activity with three spinners, 1: with the option of quantity (numbers), 2: with products and 3: with the price of the selected product and a button to add to the list, I have my custom listview and the data with the get, set and my constructor but I have problems to put them in the adapter and show them in the listview doing the corresponding operation every time I press the add button

ListAdapter class

public class ListAdapter extends ArrayAdapter<Data> {
ArrayList<Data> versionList;
LayoutInflater vi;
int Resource;

public ListAdapter(Context context, int resource, ArrayList<Data> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    versionList = objects;
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return 500;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    final ViewHolder holder;

    if (v == null) {
        v = vi.inflate(R.layout.view_row, null);
        holder = new ViewHolder();
       holder.getCanti=(TextView)v.findViewById(R.id.getcanti);
        holder.getProd=(TextView)v.findViewById(R.id.getpresentacion);
        holder.getPre=(TextView)v.findViewById(R.id.getnameprod);

        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.getCanti.setText(versionList.get(position).getCanti());
    holder.getProd.setText(versionList.get(position).getProd());
    return v;
}

private static class ViewHolder {

    public TextView getCanti;
    public TextView getProd;
    public TextView getPre;
}

MainActivity class

public class PedidoActivity extends AppCompatActivity  implements AdapterView.OnItemSelectedListener {
private Spinner spinnerPro, spinnerLoc;

Spinner spCantidad,spProducto,spPrecio;

ArrayList<Data> dataList;

ListView listView;

ListAdapter listAdapter;

Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pedido);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    spCantidad = (Spinner) findViewById(R.id.spCantidad);
    spProducto = (Spinner) findViewById(R.id.spinnerProvincia);
    spPrecio = (Spinner) findViewById(R.id.spinnerLocalidad);
    listView = (ListView) findViewById(R.id.list);
    button = (Button) findViewById(R.id.button);
    listAdapter = new ListAdapter(getApplicationContext(), R.layout.view_row, dataList);



    dataList = new ArrayList<Data>();

    listAdapter = new ListAdapter(getApplicationContext(), R.layout.view_row, dataList);

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String cant= spCantidad.getSelectedItem().toString();
            String prod= spProducto.getSelectedItem().toString();
            String prec= spPrecio.getSelectedItem().toString();



            Data version = new Data();

            version.setCanti(cant);
            version.setCanti(prod);
            version.setCanti(prec);

            dataList.add(version);
            listView.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();
        }
    });

    listView.setAdapter(listAdapter);
    listAdapter.notifyDataSetChanged();

Data class

public class Data {
private String canti;
private String prod;
private String precio;

public Data(String canti, String prod, String precio) {
    this.canti = canti;
    this.prod = prod;
    this.precio = precio;
}

public Data() {

}

public String getCanti() {
    return canti;
}

public void setCanti(String canti) {
    this.canti = canti;
}

public String getProd() {
    return prod;
}

public void setProd(String prod) {
    this.prod = prod;
}

public String getPrecio() {
    return precio;
}

public void setPrecio(String precio) {
    this.precio = precio;
}

}

    
asked by Cesar 08.01.2018 в 07:21
source

1 answer

0

You are doing the following:

dataList.add(version); // basta con agregar el elemento
listView.setAdapter(listAdapter); // Estas haciendo un set de un adapter viejo
listAdapter.notifyDataSetChanged();

To add the new object to the list and reflect it in the Adapter that you have already set, you must do:

dataList.add(tempList);
listAdapter.notifyDataSetChanged();
    
answered by 08.01.2018 в 15:53