Recover Spinner position to use in another Spinner of another Acitivity

1

I have a note registration activity that has 1 spinner. In this Spinner contains card registration with IDCartão, title and description (Except in a "Card" table in the database). When you save this record from Notes in the "Notes" table, only the ID card is saved from that spinner.

I have another activity that edits this register of Notes, it has the same spinner, but I want this spinner already in the position of the item to be edited, but not able to do this, does anyone have any solution?

Note: Can you get the position of the item from IdCart?

Thank you in advance.

RecordSpinnerCartaoAdapter.java:

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

}

HmAuxCartao.java:

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

NotesEditActivity.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) {
    //Não Está funcioando essa parte.
                       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);


            }
    //Essa parte do código não tá funcionando, tentei fazer assim pra pegar a posição apartir do IDCartão;
            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;
            }
}

NotesDao.java:

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

TarjetaDao.java:

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 is the Models, if I need to post here. I just put the essential code.

    
asked by anonymous 20.12.2018 / 01:07

2 answers

1

In NotesEditActivity.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 Spinner has the 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.

    
20.12.2018 / 20:07
1

Try to get the position of the item as follows:

sp_card.setSelection(sp_card.getAdapter().indexOf(String.valueOf(idCartao)));
    
20.12.2018 / 16:55