我有一个带有1个微调器的“发票”注册活动。在此Spinner中包含使用IDCard进行的卡注册,标题和描述(保存在数据库的“卡”表中)。当我将此“发票”记录保存在“发票”表中时,此微调器仅保存IDCard。
我还有另一个活动可以编辑此发票记录,它具有相同的微调器,但是我希望该微调器恢复要编辑的项目的位置,但是我不能这样做,有人可以解决吗? / p>
注意:您可以从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;
}
}
要完成代码,有模型,如果我需要将其放在这里。我只输入了基本代码。
答案 0 :(得分: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 <HMAuxCartao>
类型的hmAux。
感谢您的帮助。