Search code examples
javaandroidsqlitespinner

Retrieve Spinner Custom position to use in another Spinner from another Acitivity


I have an activity of register of "Invoices" that has 1 spinner. In this Spinner contains card registration with IDCard, title and description (being saved in a "Card" table in the database). When I save this "Invoices" record in the "Invoices" table, this spinner only saves the IDCard.

I have another activity that edits this register of Invoices, it has the same spinner, but I want this spinner to recover the position of the item to be edited, but I can not do this, does anyone have a solution?

Note: Can you get the position of the item from the IdCard? Sorry for my bad english.

Thanks in advance.

RecordSpinnerCardAdapter.class:

public class RecordSpinnerCartaoAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private ArrayList<HMAuxCartao> hmAux;

    public RecordSpinnerCartaoAdapter(Context context, int layout, ArrayList<HMAuxCartao> hmAux) {
        this.hmAux = hmAux;
        this.context = context;
        this.layout = layout;
    }

    @Override
    public int getCount() {
        return hmAux.size();
    }

    @Override
    public Object getItem(int i) {
        return hmAux.get(i);
    }

    @Override
        public long getItemId(int i) {
            return i;
        }

    private class ViewHolder{

        TextView celula_cartao, celula_number;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        View row = view;
        ViewHolder holder = new ViewHolder();

        if (row==null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(layout, viewGroup,false);
            holder.celula_cartao = row.findViewById(R.id.celula_cartao);
            holder.celula_number = row.findViewById(R.id.celula_number);

            row.setTag(holder);
        }
        else {
            holder = (ViewHolder)row.getTag();
        }
//monta a listview
        HMAuxCartao model = hmAux.get(i);

      holder.celula_cartao.setText(model.get(CartaoDao.DESCARTAO));
        holder.celula_number.setText(model.get(CartaoDao.NUMBERCARD));

                return row;
    }

}

HmAuxCard.class

public class HMAuxCartao extends HashMap<String, String> {
    @Override
    public String toString() {
        return get(CartaoDao.DESCARTAO);
    }
}

InvoicesEditActivity.java:

    public class NotasEditActivity extends AppCompatActivity {

        private Context context;
        private NotasDao notasDao;
        private CartaoDao cartaoDao;
        private RecordSpinnerCartaoAdapter adapter;
        //
        private Spinner sp_card;
        //
        private int idCartao;
        //
        private long idAtual;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.note_view_screen);

            iniciarVariaveis();
            iniciarAcoes();
        }

        private void iniciarVariaveis() {
            context = getBaseContext();
            notasDao = new NotasDao(context);
            cartaoDao = new CartaoDao(context);
            recuperarParametros();

            sp_card = findViewById(R.id.sp_card);

            adapter = new RecordSpinnerCartaoAdapter(context, R.layout.celula_spinner_card_layout, cartaoDao.obterListaCartao());
            sp_card.setAdapter(adapter);

        }

        private void iniciarAcoes() {
            if (idAtual != -1) {

                Notas cAux = notasDao.obterNotasById(idAtual);

                idCartao = (int) cAux.getIdcartao();

              sp_card.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
               @Override
               public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//That part is not working.
                   sp_card.setSelection(getSpinnerIndex(sp_card, String.valueOf(idCartao)));
               }
               @Override
               public void onNothingSelected(AdapterView<?> parent) {
                   sp_card.setSelection(getSpinnerIndex(sp_card, String.valueOf(idCartao)));
               }
           });
            }
        }

        private void recuperarParametros() {
            idAtual = getIntent().getLongExtra(Constantes.ID_BANCO, 0);


        }
//This part of the code is not working, I tried to do so to get the position from the IDCard;
        public static int getSpinnerIndex(Spinner spinner, String myString){
            int index = 0;
            for (int i=0;i<spinner.getCount();i++){
                if (spinner.getItemAtPosition(i).toString().equals(myString)){
                    index = i;
                }
            }
            return index;
        }

InvoicesDao.class

public class NotasDao extends Dao {

    private static final String TABELANOTAS = "notas";
    public static final String IDNOTAS = "idnotas";
    public static final String IDCARTAO = "idcartao";    

    public NotasDao(Context context) {
        super(context);
    }

    public Notas obterNotasById(long idnotas) {
        Notas cAux = null;
        //
        abrirBanco();
        //
        Cursor cursor = null;
        //
        try {

            String comando = " select * from " + TABELANOTAS + " where " + IDNOTAS + " = ? ";
            String[] argumentos = {String.valueOf(idnotas)};
            //
            cursor = db.rawQuery(comando, argumentos);
            //
            while (cursor.moveToNext()) {
                cAux = new Notas();
                cAux.setIdnotas(cursor.getLong(cursor.getColumnIndex(IDNOTAS)));
                cAux.setIdcartao(cursor.getLong(cursor.getColumnIndex(IDCARTAO)));
            }
            //
            cursor.close();

        } catch (Exception e) {
            Log.e(TAG, "obterNotasById: ");
        }
        //
        fecharBanco();
        //
        return cAux;
    }
}

CardDao.class

public class CartaoDao extends Dao {

    private static final String TABELA = "cartao";
    public static final String IDCARTAO = "idcartao";
    public static final String DESCARTAO = "descartao";
    public static final String NUMBERCARD = "numbercard";

    public CartaoDao(Context context) {
        super(context);
    }

    public ArrayList<HMAuxCartao> obterListaCartao() {
        ArrayList<HMAuxCartao> cartao = new ArrayList<>();
        //
        abrirBanco();
        //
        Cursor cursor = null;
        //
        try {
            String comando = " select " + IDCARTAO + ", " + DESCARTAO + ", " + NUMBERCARD  + " from " + TABELA + " order by " + DESCARTAO + " ";
            //
            cursor = db.rawQuery(comando, null);
            //
            while (cursor.moveToNext()) {
                HMAuxCartao hmAux = new HMAuxCartao();

                hmAux.put(IDCARTAO, cursor.getString(cursor.getColumnIndex(IDCARTAO)));
                hmAux.put(DESCARTAO, cursor.getString(cursor.getColumnIndex(DESCARTAO)));
                hmAux.put(NUMBERCARD, cursor.getString(cursor.getColumnIndex(NUMBERCARD)));
                //
                cartao.add(hmAux);
            }
            //
            cursor.close();

        } catch (Exception e) {
            Log.e(TAG, "obterListaCartao: ");
        }
        //
        fecharBanco();
        //
        return cartao;
    }
}

To complete the code, there are Models, if I need to put them here. I just put the essential code.


Solution

  • In InvoicesEditActivity.java I changed the getSpinnerIndex method to:

    public int getSpinnerIndex(Spinner spinner, String myString) {
        int index = 0;
    
        for (int i = 0; i < spinner.getCount(); i++) {
            HMAuxCartao model = hmAux.get(i);
            String modelS = model.get(CartaoDao.IDCARTAO);
            if (modelS != null) {
                if (modelS.equals(myString)) {
                    index = i;
                }
            }
        }
        return index;
    }
    

    In the same activity where it has Spinner action I deleted everything and just added:

        hmAux = cartaoDao.obterListaCartao();
        sp_card.setSelection(getSpinnerIndex(sp_card, String.valueOf(idCartao)));
    

    Being this hmAux of type ArrayList <HMAuxCartao>.

    Thanks for the help.