我有一个 Spinner
在我的 Activity
当我点击一个项目时,什么也没有发生。
这样做的目的Activity
是搜索电话联系人。它可以在 SQLite
中按姓名或电话号码搜索数据库。可以想象,如果我按姓名搜索,选择可以找到两个同名的联系人,如果发生这种情况,我想将选项放在微调器上以选择我想查看的联系人。一旦我选择了 cootact,就应该在 EditText
上设置信息领域,但它不会发生。
这里的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buscar);
Conexion=new BaseDeDatos(this, "BDCONTACTOS", null, 1);
alerta = new AlertDialog.Builder(this).create();
if(Conexion==null){
alerta.setMessage("LA conexion NO se ha hecho");
alerta.show();
return;
}
BD = Conexion.getWritableDatabase();
nombreTxt = (EditText)findViewById(R.id.editText1);
telefonoTxt = (EditText)findViewById(R.id.editText2);
direccionTxt = (EditText)findViewById(R.id.editText3);
buscarBtn = (Button)findViewById(R.id.button1);
limpiarBtn = (Button)findViewById(R.id.button2);
miSpinner = (Spinner)findViewById(R.id.spinner1);
adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
miSpinner.setAdapter(adp);
miSpinner.setDropDownVerticalOffset(50);
miSpinner.setOnItemSelectedListener(this);
buscarBtn.setOnClickListener(this);
limpiarBtn.setOnClickListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int index = miSpinner.getSelectedItemPosition();
nombreTxt.setText(registros[index][0]);
telefonoTxt.setText(registros[index][1]);
direccionTxt.setText(registros[index][2]);
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
public void onClick(View v) {
if(v==buscarBtn){
BD = Conexion.getWritableDatabase();
if(BD==null){
alerta.setTitle("Mensaje");
alerta.setMessage("La base de datos no está abierta");
alerta.show();
return;
}
nombre= nombreTxt.getText().toString();
telefono=telefonoTxt.getText().toString();
direccion=direccionTxt.getText().toString();
if(nombre.equals("")&&telefono.equals("")){
alerta.setTitle("Mensaje");
alerta.setMessage("Se necesita nombre ó teléfono para buscar");
alerta.show();
return;
}
Cursor c;
if(nombre.equals("")&&!telefono.equals("")){
String telefono=telefonoTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Telefono='"+telefono+"'", null);
}
if(telefono.equals("")&&!nombre.equals("")){
String nombre=nombreTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Nombre='"+nombre+"'", null);
}else{
String telefono=telefonoTxt.getText().toString();
String nombre=nombreTxt.getText().toString();
c = BD.rawQuery("SELECT Nombre,Telefono,Direccion FROM MisContactos WHERE Nombre='"+nombre+"' and Telefono='"+telefono+"'", null);
}
int count=c.getCount();
alerta.setMessage("Registros encontrados ="+count);
alerta.show();
if(count==0)
return;
registros=new String[count][3];
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
int i=0;
do {
// nombreTxt.setText(c.getString(0));
registros[i][0]=c.getString(0);
//telefonoTxt.setText(c.getString(1));
registros[i][4]=c.getString(1);
//direccionTxt.setText(c.getString(2));
registros[i][5]=c.getString(2);
i++;
} while(c.moveToNext());
}
if(miSpinner.getAdapter().getCount()>0){
arrayList1=new ArrayList<String>();
adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
miSpinner.setAdapter(adp);
}
for(int j=0;j<count;j++)
arrayList1.add("Ver contacto "+(j+1));
return;
}
if(v==limpiarBtn){
limpiar();
return;
}
}
public void limpiar(){
nombreTxt.setText("");
telefonoTxt.setText("");
direccionTxt.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buscar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
一些截图:
一见钟情的 Activity :
一旦我按名称搜索,它发现了两行:
如果我从微调器中单击一个选项(但它没有),它应该设置名称、电话号码和地址(名词、电话号码、方向)
这是我第一次在这个网站上提问。谢谢您的支持。
最佳答案
这是简单的方法 在您的按钮点击中试试这个
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
或者
选择某些内容时,Spinner 应触发“OnItemSelected”事件:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
关于java - onItemSelectedListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627790/