I am developing an application where I use a RecyclerView
to display a list of elements. Each item
of this list is personalized, consisting of textview
a spinner
and% editText
. The purpose is to update the data of the elements of that list in my database. The problem is that when I go through the list and try to update these data I take the update of the last modified iten, that is, what is selected in my spinner
and editText
in the other items. Let's see if someone can help me.
ConfiguraçãoObraSocial.java
public class ConfiguracionOS extends AppCompatActivity {
BaseDeDatos db;
RecyclerView osView;
LinearLayout cancelar, actualizar;
int contador;
Context context;
String nombre, est, cos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuracion_os);
context = ConfiguracionOS.this;
db = new BaseDeDatos(context,null,null,1);
osView = (RecyclerView) findViewById(R.id.configuracionOS);
osView.setLayoutManager(new LinearLayoutManager(context));
osView.setItemAnimator(new DefaultItemAnimator());
ControladorObraSocial controlador = new ControladorObraSocial(context);
final ConfiguracionOSAdapter adapter = new ConfiguracionOSAdapter(context, controlador.listaObraSocial());
osView.setAdapter(adapter);
cancelar = (LinearLayout) findViewById(R.id.btnVolverGestionarOS);
actualizar = (LinearLayout) findViewById(R.id.btnActualizarEstadoOS);
cancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
actualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ControladorObraSocial controlador = new ControladorObraSocial(context);
contador = osView.getAdapter().getItemCount();
for (int i=0; i < contador; i++ ) {
nombre = adapter.nombre;
cos = adapter.coseguro;
est = adapter.estado;
controlador.setDatos(nombre, est, cos);
controlador.cargarDatos();
}
finish();
}
});
}
}
ControllerOS.java
public class ControladorObraSocial {
Context context;
public String est, cos, nom;
public ControladorObraSocial(Context c) {
this.context = c;
}
public void cargarDatos(){
BaseDeDatos conn = new BaseDeDatos(context,null,null,1);
ContentValues values = new ContentValues();
values.put(Tablas.COLUMNA_ESTADO_OS, est);
values.put(Tablas.COLUMNA_COSEGURO_OS, cos);
SQLiteDatabase bd = conn.getWritableDatabase();
String[] parametro = {nom};
bd.update(Tablas.TABLA_OS, values, Tablas.COLUMNA_NOMBRE_OS + "= ?", parametro);
bd.close();
}
public void setDatos(String nombre, String estado, String coseguro){
nom = nombre;
est = estado;
cos = coseguro;
}
SettingsAdapter.java
public class ConfiguracionOSAdapter extends RecyclerView.Adapter<ConfiguracionOSHolder> implements Filterable {
Context c;
public ArrayList<ObraSocial> OS, listaFiltrada;
FiltroConfiguracionOS filtro;
public String coseguro, estado, nombre;
public ConfiguracionOSAdapter(Context ctx, ArrayList<ObraSocial> OS){
this.c=ctx;
this.OS=OS;
this.listaFiltrada=OS;
}
@Override
public ConfiguracionOSHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_configuracion_os,null);
//HOLDER
ConfiguracionOSHolder holder=new ConfiguracionOSHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final ConfiguracionOSHolder holder, int position) {
holder.nombreOS.setText(OS.get(position).getNombre());
holder.estado.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
estado = holder.estado.getSelectedItem().toString();
nombre = holder.nombreOS.getText().toString();
if (position == 1) {
holder.coseguroOS.setEnabled(true);
}else {
holder.coseguroOS.setText("");
holder.coseguroOS.setEnabled(false);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
holder.coseguroOS.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
coseguro = holder.coseguroOS.getText().toString();
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
}
@Override
public int getItemCount() {
return OS.size();
}
@Override
public Filter getFilter() {
if (filtro==null){
filtro = new FiltroConfiguracionOS(listaFiltrada, this);
}
return filtro;
}
}
ConfigurationsOSHolder
public class ConfiguracionOSHolder extends RecyclerView.ViewHolder {
public TextView nombreOS;
public Spinner estado;
public EditText coseguroOS;
public ConfiguracionOSHolder(View itemView) {
super(itemView);
this.nombreOS = (TextView) itemView.findViewById(R.id.NombreOS);
this.estado = (Spinner) itemView.findViewById(R.id.spEstadosOS);
this.coseguroOS = (EditText) itemView.findViewById(R.id.CoseguroOS);
}
}