Android 如何从spinner(mysql\u id)检索一个信息

Android 如何从spinner(mysql\u id)检索一个信息,android,spinner,Android,Spinner,首先,stackoverflow是我所在大学访问量最大的网站。 嗯,我有一个微调器,里面有一些信息,我正在尝试做一个消息应用程序。让我们看看代码: public void fillEmployee() { EmployeeDAO dao = new EmployeeDAO(getBaseContext()); spnDestinationMsg = (Spinner) findViewById(R.id.spnDestinationMsg); String query =

首先,stackoverflow是我所在大学访问量最大的网站。 嗯,我有一个微调器,里面有一些信息,我正在尝试做一个消息应用程序。让我们看看代码:

public void fillEmployee() {
    EmployeeDAO dao = new EmployeeDAO(getBaseContext());
    spnDestinationMsg = (Spinner) findViewById(R.id.spnDestinationMsg);
    String query = "SELECT * FROM employee ORDER BY mysql_id ASC";

    Cursor cursor = dao.cacthByCursor(query);
    String[] from = { "mysql_id", "name" };
    int[] to = { R.id.txtMysql_Id_employee, R.id.txtName_employee };

    SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.item_employee, cursor, from, to, 0);
    spnDestinationMsg.setAdapter(ad);}
在我的对象Employee中,我有以下字段:

    private long id;
    private long mysql_id;
    private String name;
    private int updatestatus;
好的,现在我必须将mysql\u id设置为employee对象,其值来自微调器。当我第一次启动应用程序时,mysql\u id来自web服务。我需要获取mysql\u id

例如:

EmployeeDAO  emp1 =  new EmployeeDAO(getBaseContext());

emp1.setMySql_id(spnDestinationMsg.getAdapter().getItemId(spnDestinationMsg .getSelectedItemPosition()));
我遇到的问题是,微调器只获取数组的值或_id的值。我需要访问mysql_id。 我该怎么做?是否有任何方法可以访问存储在微调器中的任何数据。因为在游标内,我选择了表中的所有内容。如何访问其他值?例如,如果我想获取updatestatus的值?我单击微调器项,捕获任何值-mysql\u id、\u id、updatestatus等。是否可能?。 PS:我不想使用OnSelectedListener,因为用户可能不会更改微调器。
感谢您的帮助。

您可以轻松地使用一个已选的侦听器,尽管您对用户没有做出更改的感觉

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...

   public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
   }

  public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
  }
}

如果用户实际上没有更改任何内容,OnNothingSelected回调将保存您为spinner创建自己的自定义适配器

public class CustomSpinnerAdapter extends BaseAdapter  {

private Activity context;

public MySpinnerAdapter (Activity context){
    this.context=context;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RelativeLayout item= (RelativeLayout)context.getLayoutInflater().inflate(R.layout.<your spinner item layout name>, null); 

  //Your data to layout components

  item.setTag(<your tag object>);

    return item;
}

}这是全部代码:

public class MsgCadActivity extends Activity implements OnItemSelectedListener{

private static final String PREF_NAME = "LoginActivityPreferences";
SharedPreferences sp;

static Spinner spnDestinoMsg;
static EditText txtAssuntoMsg;
static EditText txtMsg;
static Button btnEnviarMsg;
static Mensagem mensagem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_msg_cad);
    populaFuncionarioDestino();

}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

public void populaFuncionarioDestino() {
    FuncionarioDAO dao = new FuncionarioDAO(getBaseContext());
    spnDestinoMsg = (Spinner) findViewById(R.id.spnDestinoMsg);
    String query = "SELECT * FROM funcionario ORDER BY mysql_id ASC";

    Cursor cursor = dao.recuperarPorQueryCursor(query);
    String[] from = { "mysql_id", "nome" };
    int[] to = { R.id.txtMysql_Id_funcionario, R.id.txtNome_funcionario };

    SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.item_funcionario, cursor, from, to, 0);
    spnDestinoMsg.setAdapter(ad);

}// endpopularFornecedor()

public void salvar(View v) {

    sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    Long funcionario_id = sp.getLong("funcionario_id", 0);      
    Log.d("MsgCadActivity ", " funcionario_id ->"+ funcionario_id.toString());
    // Linkar xml com objetos
    spnDestinoMsg = (Spinner) findViewById(R.id.spnDestinoMsg);
    txtAssuntoMsg = (EditText) findViewById(R.id.txtAssuntoMsg);
    txtMsg = (EditText) findViewById(R.id.txtMsg);
    btnEnviarMsg = (Button) findViewById(R.id.btnEnviarMsg);

    mensagem = new Mensagem();

    // VALIDACOES
    if ((Util.validateNotNull(txtAssuntoMsg, "Preencha o Assunto!"))
            & (Util.validateNotNull(txtMsg, "Preencha a Mensagem!"))) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateandTime = sdf.format(new Date());

        mensagem.setAssunto(txtAssuntoMsg.getEditableText().toString());
        mensagem.setMsg(txtMsg.getEditableText().toString());
        mensagem.setFuncionario_origem_id(funcionario_id);          

        mensagem.setEnviado(currentDateandTime);

        MensagemDAO dao =  new MensagemDAO(getBaseContext());

        //dao.salvar(mensagem);
        //Toast.makeText(this, "Mensagem enviada com Sucesso!", Toast.LENGTH_LONG).show();
        //dao.fecharConexao();
        //finish();
    }// end if

}// end salvarFM()

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

        int pos =  (Integer) parent.getItemAtPosition(position);
        Log.d("MsgCadActivity ", " pos ->"+ pos);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}

}

谢谢你的帮助,马丁。对不起,我在这里做了,但它不起作用。请澄清你所说的不起作用是什么意思。以上是Android文档中的内容,这部分代码永远不会到达:int pos=Integer parent.getItemAtPositionposition;Log.dmsgadactivity,pos->+pos;有没有办法存储和恢复微调器中的所有数据?有简单的方法吗?对不起,伙计们,我有很多问题要适应android。我来自Windows development=HeuHi交换IOS Android。谢谢你的回答。但是你有简单的方法吗?我是android开发的初学者,我真的很想在一开始就使用简单的解决方案。谢谢你的帮助。