I'm trying to count the amount clicksa person does on a given item item
in AndroidStudioListView
by displaying it in a item within the same list. For example, if I press the , the counter will increase according to the number of times I press the . If I press item b, it will increase the counter of item b and so on. What I mean specifically is that if I press item b the counter displays its value in item a and I want it to display in item b. I leave catch.textview
item a
item a
public class Mozos extends AppCompatActivity{
AlmacenConexion aC;
Spinner spinMozo;
List<String> listaNombres, listacomidas;
ArrayAdapter<String> adapterSpin;
ArrayList<Comidas> arrayComidas;
adaptadorComidas adap;
ListView lvCom;
TextView tv;
ArrayList<Integer> c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mozos);
//Spinner
spinMozo=(Spinner)findViewById(R.id.spinMozo);
aC=AlmacenConexion.getInstance(this);//para que podamos usar los métodos de la clase AlmacenConexion
listaNombres =new ArrayList<>();//conversion de List a ArrayList
int tamañoLista =aC.todosMozos().size(); //almacena el tamaño
listaNombres.add("Mozo");
for(int i=0;i<tamañoLista;i++){//agregamos los nombres y los almacanamos el la lista
listaNombres.add(aC.todosMozos().get(i).getNombre());
}
adapterSpin = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listaNombres);//implementamos un adapter con el contexto
spinMozo.setAdapter(adapterSpin);//cargamos los datos
//tv
//ListView
arrayComidas = new ArrayList<Comidas>();
lvCom=(ListView)findViewById(R.id.lvCom);
arrayComidas=aC.llenar_Comidas();
c = new ArrayList<>();
for (int i = 0; i < arrayComidas.size(); i++) {
c.add(0);
}
adap= new adaptadorComidas(this, arrayComidas);
lvCom.setAdapter(adap);
lvCom.setClickable(true);
lvCom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tv=(TextView)findViewById(R.id.lbl_cantidad_producto);
aC = new AlmacenConexion(getApplicationContext());
int contador = c.get(position);
contador++;
c.set(position,contador);
if(position==0){
tv.setText(""+(c.get(position)));
System.out.println("position:" + position);
}else if(position==1){
tv.setText(""+(c.get(position)));
System.out.println("position:" + position);
}
}
});
}
}
gifs
And this is the adapter:
public class adaptadorComidas extends BaseAdapter {
private Context c;
ArrayList<Comidas> arraComi;
public adaptadorComidas(Context context, ArrayList<Comidas> comidas){
this.c=context;
this.arraComi=comidas;
}
@Override
public int getCount(){
return this.arraComi.size();
}
@Override
public Object getItem(int position){
return this.arraComi.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//creamos la vista
View rowView = convertView;
if (convertView == null) {
// Nueva vista en la lista
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item_list_general, parent, false);//La metemos en el layout
//Que es una vista creada
}
// Mostramos los datos
TextView nombreProducto= rowView.findViewById(R.id.lbl_nombre_producto); //en un textview
TextView precioProducto= rowView.findViewById(R.id.lbl_precio_producto); //en un textview
Comidas comidas = this.arraComi.get(position); //llamamos a la clase según la posición
nombreProducto.setText(comidas.getDescr());//traemos los valores a mostrar
precioProducto.setText(comidas.getPre());//traemos los valores a mostrar
return rowView;
}
So far, this only works for the first item in the list. That is, I have the item a and pressing on it increases the counter. But, if I press the one item b
in the list it keeps increasing the counter of theitem a.
You could create an array of counters, and each time you press one you
item
update the counter for that position.Your Array of counters:
You initialize your array of counters according to how many
items
you have:When they do Clickon the item you update the counter of that position with
and you change the textview with
In the end your code would be more or less like this with the suggested changes: