我的ListView适配器不会更新

时间:2014-10-12 22:29:45

标签: java android listview android-adapter

我有一个Dialog,用户选择一个选项并更改de adapter,问题是适配器没有更新,我调用的函数返回ItemAdapter然后我更新我的适配器,如setListAdapter(newOne);并且它没有更新,这是完整的代码

public class MyActivity extends ListActivity {

char[] items = { 'e', 'f', 'f', 'f', 'e', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'p', 'e', 'e', 'p', 'f', 'f', 'f', 'e', 'e', 'a', 'f', 'p', 'f', 'e', 'e', 'f', 'f', 'p', 'f', 'e', 'a', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'f', 'f', 'a', 'p', 'e', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'p', 'f', 'e', 'f', 'e', 'f' };
int[] horas =   {00, 01, 03, 04, 04, 05, 05, 05, 06, 06, 06, 06, 07, 07, 07, 07, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23,};
int[] minutos = {20, 00, 00, 00, 30, 10, 25, 40, 10, 20, 40, 50, 00, 10, 30, 50, 00, 20, 40, 00, 10, 20, 30, 50, 10, 20, 40, 00, 20, 30, 40, 50, 10, 30, 40, 10, 20, 30, 40, 50, 00, 00, 10, 30, 50, 00, 10, 20, 30, 40, 00, 10, 30, 50, 50, 00, 10, 20, 20, 30, 40, 00, 20, 30, 30, 50, 10, 20, 20, 30, 30, 40, 50, 00, 20, 40, 50, 50, 10, 15, 30, 00, 40, 10, 50};
String[] total = new String[horas.length];
String[] tipos = {"Etacer", "Fluviales", "Fluviales al pozo", "Fluviales a abogacia"};

// declare class variables
private ArrayList<Item> m_parts = new ArrayList<Item>();
private Runnable viewParts;
private ItemAdapter m_adapter;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    for (int i = 0; i < items.length; i++) {
        total[i] = String.format("%02d",horas[i]) + ":" + String.format("%02d",minutos[i]);
    }

    int cuantos_f = cuantosF('f');

    // instantiate our ItemAdapter class
    m_adapter = new ItemAdapter(this, R.layout.row, m_parts);

    for (int i = 0; i < items.length; i++) {
        m_parts.add(new Item(horas[i], minutos[i], items[i], total[i]));
    }

    m_adapter = new ItemAdapter(MyActivity.this, R.layout.row, m_parts);

    // display the list.
    setListAdapter(m_adapter);

    RippleView mButton = (RippleView) findViewById(R.id.btn);
    final RippleView filtrar = (RippleView) findViewById(R.id.filtrar);
    filtrar.setRippleColor(Color.parseColor("#2ecc71"), 0.8f);
    mButton.setRippleColor(Color.parseColor("#2ecc71"), 0.8f);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your code

            mostrarSiguiente();
        }
    });

    filtrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
            builder.setTitle("Pick a color");
            builder.setItems(tipos, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ItemAdapter newOne = filtrar_tipo(which);
                    setListAdapter(newOne);  // Here it should update
                }
            });
            builder.show();
        }
    });

    final Handler mHandler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                            m_adapter.notifyDataSetChanged();
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();

}

private ItemAdapter filtrar_tipo(int i) {
    /* 0 etacer - 1 Fluvales - 2 fluviales al pozo - 3 fluv abogacia  */

    ItemAdapter adapter;

    char letra = 'g';
    int contador;
    switch (i){
        case 0: letra = 'e'; break;
        case 1: letra = 'f'; break;
        case 2: letra = 'p'; break;
        case 3: letra = 'a'; break;
    }
    contador = cuantosF(letra);

    Log.e("Contador", String.valueOf(contador));
    ArrayList<Integer> minutosf = new ArrayList<Integer>();
    ArrayList<Integer> horasf = new ArrayList<Integer>();
    ArrayList<String> fin = new ArrayList<String>();
    ArrayList<Character> letras = new ArrayList<Character>();


    for (int h = 0; h < contador; h++) {
        minutosf.add(h,minutos[h]);
        horasf.add(h,horas[h]);
        letras.add(h,items[h]);
    }
    int[] minutosFinales = aArray(minutosf);
    int[] horasFinales = aArray(horasf);

    for (int y = 0; y < contador; y++) {
        fin.add(String.format("%02d",horasFinales[y]) + ":" + String.format("%02d",minutosFinales[y]));
    }
    String[] listaFinal = fin.toArray(new String[fin.size()]);


    adapter = new ItemAdapter(this, R.layout.row, m_parts);

    for (int t = 0; t < contador; t++) {
        m_parts.add(new Item(horasFinales[t], minutosFinales[t], letra, listaFinal[t]));
    }

    adapter = new ItemAdapter(MyActivity.this, R.layout.row, m_parts);

    // display the list.
    return adapter;
}

private int cuantosF(char mi_char) {
    int contador = 0;
    for (int j = 0; j < items.length; j++) {
        if (items[j] == mi_char){
            contador++;
        }
    }
    return contador;
}

private int[] aArray(List<Integer> integers) {
    int[] ints = new int[integers.size()];
    int i = 0;
    for (Integer n : integers) {
        ints[i++] = n;
    }
    return ints;
}

private void mostrarSiguiente() {

    Calendar rightNow = Calendar.getInstance();
    int hour = rightNow.get(Calendar.HOUR_OF_DAY);
    int min = rightNow.get(Calendar.MINUTE);

    int mejor_pos = 999, menor_resta = 1500;

    for (int i = 0; i < horas.length; i++) {
        if((horas[i]*60 + minutos[i] - (hour*60+min)) > 0){
            if ((horas[i]*60 + minutos[i] - (hour*60+min)) < menor_resta){
                mejor_pos = i;
                menor_resta = (horas[i]*60 + minutos[i] - (hour*60+min));
            }
        }
    }

    ListView lv = getListView();
    lv.smoothScrollToPosition(mejor_pos);
}
}

如果有人能帮助我,那会让我开心:)

先谢谢

0 个答案:

没有答案