【问题标题】:Retrieve Spinner Custom position to use in another Spinner from another Acitivity从另一个 Activity 中检索 Spinner 自定义位置以在另一个 Spinner 中使用
【发布时间】:2018-12-21 03:13:47
【问题描述】:

我有一个“发票”登记活动,其中有 1 个微调器。在此 Spinner 中包含带有 IDCard 的卡片注册、标题和描述(保存在数据库中的“卡片”表中)。当我在“发票”表中保存此“发票”记录时,此微调器仅保存 IDCard。

我有另一个活动编辑这个发票登记册,它有相同的微调器,但我希望这个微调器恢复要编辑的项目的位置,但我不能这样做,有人有解决方案吗?

注意:您可以从 IdCard 中获取项目的位置吗? 对不起我的英语不好。

提前致谢。

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

为了完成代码,有模型,如果我需要把它们放在这里。我只是放了基本代码。

【问题讨论】:

  • 你使用了一个数组列表。正确的?然后你可以通过检查 id 是否相同来获得位置。 if(arraylist.get(position)==ID) --> 循环执行。
  • 感谢您的提示。

标签: java android sqlite spinner


【解决方案1】:

在 InvoicesEditActivity.java 中,我将 getSpinnerIndex 方法更改为:

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

在具有 Spinner 操作的同一活动中,我删除了所有内容并添加了:

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

作为ArrayList &lt;HMAuxCartao&gt; 类型的这个hmaux。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    相关资源
    最近更新 更多