Recover Spinner’s position to use in another Spinner from another Acitivity

Asked

Viewed 167 times

1

I have a Banknote Registration App that has 1 spinner. Spinner contains card registration with Idcard, title and description (Saved in a "Card" table in the database). When save this register from Notes in the "Notes" table, only the Idcard is saved from this spinner.

I have another Activity that edits this register of Notes, it has the same spinner, but I want that spinner is already in the position of the item to be edited, but not being able to do it, someone has some solution?

Note: Do you have how to get the position of the item from the Idcard?

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

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

Noteworthy.:

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

Letter from.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 have the Models, if need I put here. I put only the essential code.

2 answers

1

Try to get the position of the item as follows:

sp_card.setSelection(sp_card.getAdapter().indexOf(String.valueOf(idCartao)));
  • I appreciate the help, but I got it some other way. My Adapter does not have the index method. Att.

1


In Notaseditactivity.java I changed the method getSpinnerIndex for:

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 has the action of Spinner I deleted everything and just added:

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

This hmAux being the type ArrayList<HMAuxCartao>.

I appreciate the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.