删除ListView项目时刷新片段

时间:2016-03-18 18:10:40

标签: android listview android-fragments

我的应用是ViewPager Fragments,而标签位于顶部。 我有4个部分,其中2个共享2个值int和float存储在SharedPreferences中。

在其中一个片段中,从数据库中收回数据并显示在ListView中! 当我添加数据时我没有问题,共享偏好更改,TextView设置为新值。

当我从ListView删除项目时出现问题。 这个ListView在删除行的“X”内有一个按钮,当我删除它时,我重新计算新的平均值并将其存储在SharedPreferences中,但TextView中的值不会随新的变化而变化导致!

这里的适配器

public class VotiEsamiAdapter extends ArrayAdapter<VotiEsami> {

    public MySQLiteHelper db;
    private List<VotiEsami> searchArrayList;
    private List<VotiEsami> esami_tot;
    SharedPreferences sharedPref;


    public VotiEsamiAdapter(Context context, int textViewResourceId, List objects ){
        super(context, textViewResourceId, objects);

        searchArrayList = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_voti_line, null);

        db = new MySQLiteHelper(getContext());

        final TextView nome = (TextView)convertView.findViewById(R.id.text_line_voto_nome);
        final TextView cfu = (TextView)convertView.findViewById(R.id.text_line_voto_cfu);
        TextView voto = (TextView)convertView.findViewById(R.id.text_line_voto_valore);

        VotiEsami m = getItem(position);

        // inflate other items here :
        ImageButton delete_row_button = (ImageButton)convertView.findViewById(R.id.delete_button_line_voti);

        delete_row_button.setOnClickListener(
                new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        searchArrayList.remove(position);
                        db.deleteSelectedVotoEsame(nome.getText().toString());
                        notifyDataSetChanged();

                        //Carico la media pesata e i cfu totale con sharedpreferences
                        sharedPref = getContext().getSharedPreferences("USR_DATA", Context.MODE_PRIVATE);

                        //Aggiorno media e CFU
                        //Get all exams inside database
                        esami_tot = db.getVotiEsami();
                        float MEDIA_PONDERATA;
                        float media_parziale;
                        float media_sommata_parziale = 0;
                        int CFU_SOMM = 0;

                        if (esami_tot.isEmpty()) {

                            CFU_SOMM = 0;
                            MEDIA_PONDERATA = 0;
                            Toast.makeText(getContext(), "EMPTY!!", Toast.LENGTH_LONG).show();

                        } else {

                            //Calcolare media totale e CFU totale
                            for (VotiEsami exams : esami_tot) {

                                int voto_temp = exams.get_voto();

                                if (voto_temp == 31) voto_temp = 30;
                                media_parziale = (voto_temp) * (exams.get_cfu());
                                CFU_SOMM += exams.get_cfu();
                                media_sommata_parziale += media_parziale;

                            }
                            MEDIA_PONDERATA = media_sommata_parziale / CFU_SOMM;

                        }
                        //Salvo i dati
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putFloat("MEDIA", MEDIA_PONDERATA);
                        editor.putInt("CFU", CFU_SOMM);

                    }

                }

        );

        nome.setText(m.get_nome_esame());
        cfu.setText("CFU: " + String.valueOf(m.get_cfu()));
        if(m.get_voto() == 31) {

            voto.setText("Voto: " + String.valueOf(m.get_voto()-1) + "L");

        }else{

            voto.setText("Voto: " + String.valueOf(m.get_voto()));

        }

        //Se la media è inferiore al 6 lo scrivo in rosso
        //altrimenti in verde
        if(m.get_voto() <= 21) {

            voto.setTextColor(getContext().getResources().getColor(R.color.rosso));

        }else if ((m.get_voto() <= 25) && (m.get_voto() >= 22)){

            voto.setTextColor(getContext().getResources().getColor(R.color.giallo));

        }else if((m.get_voto() >= 25) && (m.get_voto() <=31)){

            voto.setTextColor(getContext().getResources().getColor(R.color.verde));

        }
        return convertView;
    }

}

这里是管理和显示此数据的片段

public class ThreeFragment extends Fragment {

    SharedPreferences sharedPref;
    public MySQLiteHelper db;
    public List<VotiEsami> esami;
    public VotiEsamiAdapter adapter; //Adapter per caricare la listview di voti

    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new MySQLiteHelper(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_three, container, false);

        //Carico la media pesata e i cfu totale con sharedpreferences
        sharedPref = getActivity().getSharedPreferences("USR_DATA", Context.MODE_PRIVATE);
        int defaultValueInt = 0;

        float SharedMedia = sharedPref.getFloat("MEDIA", defaultValueInt);
        int SharedCFU = sharedPref.getInt("CFU", defaultValueInt);

        TextView media_top = (TextView) view.findViewById(R.id.text_media);
        TextView cfu_top = (TextView) view.findViewById(R.id.text_tot_cfu);

        media_top.setText("Media: " + String.valueOf(SharedMedia));
        cfu_top.setText("CFU TOT: " + String.valueOf(SharedCFU));

        //Find the listview
        final ListView listView = (ListView)view.findViewById(R.id.voti_listView);

        //Get all exams inside database
        esami = db.getVotiEsami();
        adapter = new VotiEsamiAdapter(getContext(), R.layout.listview_voti_line, esami);

        listView.setAdapter(adapter);

        float MEDIA_PONDERATA;
        float media_parziale;
        float media_sommata_parziale = 0;
        int CFU_SOMM = 0;

        if(!esami.isEmpty()) {
            //Calcolare media totale e CFU totale
            for (VotiEsami exams : esami) {

                int voto_temp = exams.get_voto();

                if (voto_temp == 31) voto_temp = 30;
                media_parziale = (voto_temp) * (exams.get_cfu());
                CFU_SOMM += exams.get_cfu();
                media_sommata_parziale += media_parziale;

            }
            MEDIA_PONDERATA = media_sommata_parziale / CFU_SOMM;
        }else{

            MEDIA_PONDERATA = 0;

        }
        //Salvo i dati
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putFloat("MEDIA", MEDIA_PONDERATA);
        editor.putInt("CFU", CFU_SOMM);

        //TODO gestire cancellazione del voto dal DB e dalla listview


        Button nuovo_voto = (Button) view.findViewById(R.id.button_add_new_voto);
        nuovo_voto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //Start Dialog for input the new vote
                Intent myIntent = new Intent(getActivity(), DialogAddVotoEsame.class);
                ThreeFragment.this.startActivityForResult(myIntent, 1);

            }
        });



        return view;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        List<VotiEsami> esami_per_media;

        if(requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {

                String result_nome = data.getStringExtra("result_name_esame"); //Take the materia from Dialog
                int result_cfu = data.getIntExtra("result_cfu_esame", 1); //Take the materia from Dialog
                int result_voto = data.getIntExtra("result_voto_esame", 1); //Take the materia from Dialog

                //Add exam with vote to DB
                db.addEsameVoto(new VotiEsami(result_nome, result_cfu, result_voto));

                esami_per_media = db.getVotiEsami();

                float MEDIA_PONDERATA;
                float media_parziale;
                float media_sommata_parziale = 0;
                int CFU_SOMM = 0;

                //TODO calcolare media totale e CFU totale
                for (VotiEsami exams : esami_per_media) {

                    int voto_temp = exams.get_voto();
                    if(voto_temp == 31) voto_temp = 30;
                    media_parziale = (voto_temp) * (exams.get_cfu());
                    CFU_SOMM += exams.get_cfu();
                    media_sommata_parziale += media_parziale;

                }
                MEDIA_PONDERATA = media_sommata_parziale/CFU_SOMM;

                //Salvo i dati
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putFloat("MEDIA", MEDIA_PONDERATA);
                editor.putInt("CFU", CFU_SOMM);
                editor.commit();

                TextView media_top = (TextView) getActivity().findViewById(R.id.text_media);
                TextView cfu_top = (TextView) getActivity().findViewById(R.id.text_tot_cfu);

                media_top.setText("Media: " + String.valueOf(MEDIA_PONDERATA));
                cfu_top.setText("CFU TOT: " + String.valueOf(CFU_SOMM));

                //Refresh list
                List<VotiEsami> newesame = db.getVotiEsami();

                //Aggiorno la Listview dell'activity con il nuovo inserimento
                esami.clear();
                esami.addAll(newesame);
                adapter.notifyDataSetChanged();

            }

        }
    }//onActivityResult

}

如果删除TextView中的一两行,如何更改ListView的值? 我删除时会出现主要问题,因为我无法findViewById TextView中的Fragments Adapter。 有办法做到这一点吗?我可以在TextView的{​​{1}}内找到Fragment吗?

2 个答案:

答案 0 :(得分:1)

我认为最好的方法是使用界面。

首先,你必须创建界面并“启动”事件:

public class VotiEsamiAdapter extends ArrayAdapter<VotiEsami> {

    private OnDeleteButtonClicked onDeleteButtonClicked;
    ...

    public VotiEsamiAdapter(Context context, int textViewResourceId, List objects, OnDeleteButtonClicked onDeleteButtonClicked){
        super(context, textViewResourceId, objects);
        searchArrayList = objects;
        this.onDeleteButtonClicked = onDeleteButtonClicked;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        ...
        delete_row_button.setOnClickListener(
                new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ...
                        //Salvo i dati
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putFloat("MEDIA", MEDIA_PONDERATA);
                        editor.putInt("CFU", CFU_SOMM);

                        onDeleteButtonClicked.onDeleteClick();
                    }
                }
        );
        ...
    }
    ...
    public interface OnDeleteButtonClicked {
        void onDeleteClick();
    }

然后你必须在片段中实现该方法:

public class ThreeFragment extends Fragment implements VotiEsamiAdapter.OnDeleteButtonClicked{
...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...
        adapter = new VotiEsamiAdapter(getContext(), R.layout.listview_voti_line, esami, this);
        ...
    }
    ... 
    @Override
    public void onDeleteClick() {
        media_top.setText("Media: " + String.valueOf(SharedMedia));
        cfu_top.setText("CFU TOT: " + String.valueOf(SharedCFU));
    }
}

希望它有所帮助。

答案 1 :(得分:0)

只需将对ThreeFragment的引用传递给VotiAdapter(在构造函数中),然后使用它来查找视图并替换文本。 对于更干净的解决方案而不是传递对片段的引用,您可以定义新的回调侦听器并通过ThreeFragment实现它。当需要通知状态变化片段时,VotiAdapter可以调用此接口的方法。