how to make a listview keep the selected elements after changing the adapter

0

public class MyDialogFragmentPqte extends DialogFragment {

private ListView lista_c;
HerramientasDB herramientasDB;
SQLiteDatabase db;
private Cursor c2, texto_v;
String a_rec, ident;
private ComplesCursorAdapterCheck adaptadorc3;
private ImageButton enviarsaco;
private AutoCompleteTextView ee_saco;


static MyDialogFragmentPqte newInstance() {
    return new MyDialogFragmentPqte();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_pqte, container, false);
    lista_c = (ListView) rootView.findViewById(R.id.lista_c);
    lista_c.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

    ee_saco = (AutoCompleteTextView)rootView.findViewById(R.id.agregar_saco);

    herramientasDB = new HerramientasDB(getActivity().getApplicationContext());
    db = herramientasDB.getWritableDatabase();
    c2 = herramientasDB.cursorx();


    adaptadorc3 = new ComplesCursorAdapterCheck(getActivity().getApplicationContext(), c2);


    lista_c.setAdapter(adaptadorc3);




    enviarsaco = (ImageButton) rootView.findViewById(R.id.saco);

    lista_c.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                
            int rte = lista_c.getCheckedItemCount();
            if (rte == 0){
                enviarsaco.setImageResource(R.drawable.tbovacio);

            }else if (rte==1){
                enviarsaco.setImageResource(R.drawable.tboll1);

            }else if (rte==2){
                enviarsaco.setImageResource(R.drawable.tboll2);

            }else if (rte==3){
                enviarsaco.setImageResource(R.drawable.tboll3);

            }else if (rte==4){
                enviarsaco.setImageResource(R.drawable.tboll4);

            }else if (rte > 4){
                enviarsaco.setImageResource(R.drawable.tbolleno);

            }
            }
    });
     ee_saco.addTextChangedListener(new TextWatcher() {

         @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {               
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

         String text_var = ee_saco.getText().toString();
            String cadena_sql = "SELECT _id AS _id, categoriamay AS categoriamayor, nombre AS nombre FROM tb_complementarios WHERE nombre LIKE '%" + text_var + "%'";
            texto_v = db.rawQuery(cadena_sql, null);
            adaptadorc3.changeCursor(texto_v); 
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });

    enviarsaco.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            long selecciones[] = lista_c.getCheckedItemIds();
            SparseBooleanArray seleccionados = lista_c.getCheckedItemPositions();
            if(seleccionados==null || seleccionados.size()==0){
                Toast.makeText(getActivity(),R.string.no_select,Toast.LENGTH_SHORT).show();
            }else {

            Intent envio_pqte = new Intent(getActivity().getApplicationContext(), MostrarPaquete.class);
                   envio_pqte.putExtra("seleccionados", selecciones);
                   startActivity(envio_pqte);

            }
        }
    });


    return rootView;

}
    
asked by Zanax Lobartis 24.02.2018 в 17:32
source

0 answers