I have a ListView
with my Adapter
. In the list view I have implemented AdapterView.OnItemClickListener
to know which item is being touched so to launch a custom dialog for every item in my List.
When I dismiss the dialog I want to update the item in the List
public class ClickListenerListPreferences implements AdapterView.OnItemClickListener {
private static final int POSICION_INFORMACION = 2;
private static final int MIN_VALUE_NUMBER_PICKER = 10;
private static final int MAX_VALUE_NUMBER_PICKER = 120;
private Context context;
private static ListSettingsViewAdapter adapter;
public ClickListenerListPreferences(Context ctx, ListSettingsViewAdapter pAdapter){
context = ctx;
adapter = pAdapter;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == ListSettingsViewAdapter.POSICION_NOMBRE){
View v = inflater.inflate(R.layout.dialog_text_view, null);
Button botonAceptar = (Button) v.findViewById(R.id.boton_aceptar);
Button botonCancelar = (Button) v.findViewById(R.id.boton_cancelar);
botonAceptar.setBackgroundResource(R.drawable.hover_selector);
botonCancelar.setBackgroundResource(R.drawable.hover_selector);
final EditText editText = (EditText)v.findViewById(R.id.editTextDialog);
AlertDialog.Builder builderDialog = new AlertDialog.Builder(context);
builderDialog.setView(v);
final AlertDialog dialog = builderDialog.show();
botonAceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
String nombre = editText.getText().toString();
editor.putString("nombre", nombre);
boolean funciona = editor.commit();
adapter.actualizarNombre(nombre);
dialog.dismiss();
}
});
botonCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
else if(position == ListSettingsViewAdapter.POSICION_DURACION_ENTRENAMIENTO || position == ListSettingsViewAdapter.POSICION_DURACION_MI_ENTRENAMIENTO){
View v = inflater.inflate(R.layout.dialog_number_picker, null);
SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
final NumberPicker picker = (NumberPicker)v.findViewById(R.id.number_picker_dialog);
picker.setMinValue(MIN_VALUE_NUMBER_PICKER);
picker.setMaxValue(MAX_VALUE_NUMBER_PICKER);
if(position == ListSettingsViewAdapter.POSICION_DURACION_ENTRENAMIENTO) {
int duracion = sp.getInt("duracionEntrenamiento", 30);
picker.setValue(duracion);
}
else if(position == ListSettingsViewAdapter.POSICION_DURACION_MI_ENTRENAMIENTO){
int duracion = sp.getInt("duracionMiEntrenamiento", 30);
picker.setValue(duracion);
}
Button botonAceptar = (Button) v.findViewById(R.id.boton_aceptar);
Button botonCancelar = (Button) v.findViewById(R.id.boton_cancelar);
botonAceptar.setBackgroundResource(R.drawable.hover_selector);
botonCancelar.setBackgroundResource(R.drawable.hover_selector);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(v);
final AlertDialog dialog = builder.show();
botonAceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if(position == ListSettingsViewAdapter.POSICION_DURACION_ENTRENAMIENTO) {
int valor = picker.getValue();
editor.putInt("duracionEntrenamiento", valor);
editor.commit();
adapter.actualizarDuracionEntrenamiento(valor);
}
else if(position == ListSettingsViewAdapter.POSICION_DURACION_MI_ENTRENAMIENTO){
int valor = picker.getValue();
editor.putInt("duracionMiEntrenamiento", valor);
editor.commit();
adapter.actualizarDuracionEntrenamiento(valor);
}
dialog.dismiss();
}
});
botonCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
}
}
And my Adapter class:
public class ListSettingsViewAdapter extends BaseAdapter {
private static Vector<ItemPreference> itemsPreference;
private Context context;
private LayoutInflater inflater; //Para acceder al los elementos del list_item.xml
private final int PRIMER_SWITCH = 5; //Posicion en la lista del primer Switch
private final int SEGUNDO_SWITCH = 8; //Posicion en la lista del segundo Switch
public static final int POSICION_NOMBRE = 1;
public static final int POSICION_DURACION_ENTRENAMIENTO = 4;
public static final int POSICION_DURACION_MI_ENTRENAMIENTO = 7;
public ListSettingsViewAdapter(Context ctx, Vector<ItemPreference> pItemsPreference){
super();
context = ctx;
itemsPreference = pItemsPreference;
}
@Override
public int getCount() {
return itemsPreference.size(); //Cantidad de elementos dentro de la lista. Si no le damos la cantidad, la lista aparecera vacia
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Accedo al .xml del item de la lista que voy a inflar
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = null;
if(itemsPreference.elementAt(position).getTipoLayout() == 0){ //Layout 2 TextViews
v = inflater.inflate(R.layout.list_item_text_view, parent, false);
v.setTag(position);
TextView titulo = (TextView)v.findViewById(R.id.titulo);
TextView subtitulo = (TextView)v.findViewById(R.id.subtitulo);
titulo.setText(itemsPreference.elementAt(position).getTitular());
subtitulo.setText(itemsPreference.elementAt(position).getDescripcion());
}
return v;
}
public void actualizarNombre(String nombre){
itemsPreference.elementAt(POSICION_NOMBRE).setDescripcion(nombre);
notifyDataSetChanged();
}
public void actualizarDuracionEntrenamiento(int duracion){
itemsPreference.elementAt(POSICION_DURACION_ENTRENAMIENTO).setDescripcion(Integer.toString(duracion));
notifyDataSetChanged();
}
public void actualizarDuracionMiEntrenamiento(int duracion){
itemsPreference.elementAt(POSICION_DURACION_MI_ENTRENAMIENTO).setDescripcion(Integer.toString(duracion));
notifyDataSetChanged();
}
}
When I call my adapter.actualizarNombre(nombre);
in my ClickListener
, it is correctly updated in my ListView
when I dismiss the dialog. But when it enters into the elseif()
and I call adapter.actualizarDuracionEntrenamiento(valor);
it is being executed but nothing happens and what most surprises me is that if I move adapter.actualizarDuracionEntrenamiento(valor);
to the same position of adapter.actualizarNombre(nombre);
it is correctly updated.
Do you have any idea of what might be happening?
I don't speak your language but it seems in your dialog's click listener, when the position is POSICION_DURACION_MI_ENTRENAMIENTO, you are calling
adapter.actualizarDuracionEntrenamiento(valor);
instead of
adapter.actualizarDuracionMiEntrenamiento(valor);