首先,stackoverflow是我大学访问最多的网站= D. 好吧,我有一个微调器有一些信息(我正在尝试做一个消息应用程序)。我们来看看代码:
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_id设置为employee对象,其值来自微调器(当我第一次启动应用程序时,mysql_id来自web服务)。我需要获取mysql_id。
例如:
EmployeeDAO emp1 = new EmployeeDAO(getBaseContext());
emp1.setMySql_id(spnDestinationMsg.getAdapter().getItemId(spnDestinationMsg .getSelectedItemPosition()));
我遇到的问题是因为微调器只是获取数组的值或_id的值。我需要访问mysql_id。 我怎样才能做到这一点?有没有办法访问存储在微调器中的任何数据。因为在光标内部我从表中选择了所有内容。如何访问其他值?如果我想抓住updatestatus的值,例如? (我点击微调项目,我捕获任何值 - mysql_id,_id,updatestatus等等。有可能吗?)。 PS:我想在不使用OnSelectedListener的情况下执行此操作,因为用户可能无法更改微调器。 感谢您的帮助。
答案 0 :(得分:0)
你可以轻松使用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”回调将为您保存
答案 1 :(得分:0)
为微调器创建自己的自定义适配器
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;
}
}
答案 2 :(得分:0)
这是整个代码:
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
}
}