BaseAdapter中的notifyDataSetChanged()无法正常工作

时间:2015-07-26 12:38:24

标签: android listview baseadapter notifydatasetchanged

我的Application有一个ListView。在列表视图中,我已实现Adapter以了解要触摸的项目,以便为列表中的每个项目启动自定义对话框。

当我关闭对话框时,我想更新列表中的项目

AdapterView.OnItemClickListener

我的Adapter类:

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();
            }
        });

        }
    }
}

当我在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(); } } 中呼叫我的adapter.actualizarNombre(nombre);时,当我关闭对话框时,我的ClickListener内的ListView已正确更新。但当它进入elseif()并且我调用adapter.actualizarDuracionEntrenamiento(valor);时它正在执行但没有任何反应,最令我惊讶的是,如果我将adapter.actualizarDuracionEntrenamiento(valor);移动到adapter.actualizarNombre(nombre);的相同位置它已正确更新。

你知道可能会发生什么吗?

1 个答案:

答案 0 :(得分:0)

我不说你的语言,但它似乎在对话框的点击监听器中,当你的位置是POSICION_DURACION_MI_ENTRENAMIENTO时,你正在调用

adapter.actualizarDuracionEntrenamiento(valor);

而不是

adapter.actualizarDuracionMiEntrenamiento(valor);
相关问题